Source code of Windows XP (NT5)
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.

442 lines
15 KiB

  1. /*++
  2. Copyright (c) 1997-2000 Microsoft Corporation
  3. Module Name:
  4. untfs2.h
  5. Abstract:
  6. This module contains partial basic declarations and definitions for
  7. the NTFS utilities. Note that more extensive description of the
  8. file system structures may be found in ntos\inc\ntfs.h.
  9. Author:
  10. Daniel Chan [danielch] 29-Mar-1997
  11. Revision History:
  12. IMPORTANT NOTE:
  13. The NTFS on-disk structure must guarantee natural alignment of all
  14. arithmetic quantities on disk up to and including quad-word (64-bit)
  15. numbers. Therefore, all attribute records are quad-word aligned, etc.
  16. --*/
  17. #if !defined( _UNTFS2_DEFN_ )
  18. #define _UNTFS2_DEFN_
  19. #include "bigint.hxx"
  20. #include "bpb.hxx"
  21. #include "wstring.hxx"
  22. #include <pshpack4.h>
  23. DEFINE_TYPE( BIG_INT, LCN );
  24. DEFINE_TYPE( BIG_INT, VCN );
  25. DEFINE_TYPE( LARGE_INTEGER, LSN );
  26. // This definition is for VCNs that appear in Unions, since an object
  27. // with a constructor (like a BIG_INT) can't appear in a union.
  28. DEFINE_TYPE( LARGE_INTEGER, VCN2 );
  29. // An MFT_SEGMENT_REFERENCE identifies a cluster in the Master
  30. // File Table by its file number (VCN in Master File Table) and
  31. // sequence number. If the sequence number is zero, sequence
  32. // number checking is not performed.
  33. typedef struct _MFT_SEGMENT_REFERENCE {
  34. ULONG LowPart;
  35. USHORT HighPart;
  36. USHORT SequenceNumber;
  37. } MFT_SEGMENT_REFERENCE, *PMFT_SEGMENT_REFERENCE;
  38. DEFINE_TYPE( struct _MFT_SEGMENT_REFERENCE, MFT_SEGMENT_REFERENCE );
  39. DEFINE_TYPE( MFT_SEGMENT_REFERENCE, FILE_REFERENCE );
  40. // System file numbers:
  41. //
  42. // The first sixteen entries in the Master File Table are reserved for
  43. // system use. The following reserved slots have been defined:
  44. #define MASTER_FILE_TABLE_NUMBER (0)
  45. #define MASTER_FILE_TABLE2_NUMBER (1)
  46. #define LOG_FILE_NUMBER (2)
  47. #define VOLUME_DASD_NUMBER (3)
  48. #define ATTRIBUTE_DEF_TABLE_NUMBER (4)
  49. #define ROOT_FILE_NAME_INDEX_NUMBER (5)
  50. #define BIT_MAP_FILE_NUMBER (6)
  51. #define BOOT_FILE_NUMBER (7)
  52. #define BAD_CLUSTER_FILE_NUMBER (8)
  53. #define QUOTA_TABLE_NUMBER (9) // for version < 2.0
  54. #define SECURITY_TABLE_NUMBER (9) // for version >= 2.0
  55. #define UPCASE_TABLE_NUMBER (10)
  56. #define EXTEND_TABLE_NUMBER (11) // for version >= 2.0
  57. #define MFT_OVERFLOW_FRS_NUMBER (15)
  58. #define FIRST_USER_FILE_NUMBER (16)
  59. DEFINE_TYPE( ULONG, ATTRIBUTE_TYPE_CODE );
  60. //
  61. // System-defined Attribute Type Codes. For the System-defined attributes,
  62. // the Unicode Name is exactly equal to the name of the following symbols.
  63. // For this reason, all of the system-defined attribute names start with "$",
  64. // to always distinguish them when attribute names are listed, and to reserve
  65. // a namespace for attributes defined in the future. I.e., a User-Defined
  66. // attribute name will never collide with a current or future system-defined
  67. // attribute name if it does not start with "$". User attribute numbers
  68. // should not start until $FIRST_USER_DEFINED_ATTRIBUTE, too allow the
  69. // potential for upgrading existing volumes with new user-defined attributes
  70. // in future versions of NTFS. The tagged attribute list is terminated with
  71. // a lone-standing $END - the rest of the attribute record does not exist.
  72. //
  73. #define $UNUSED (0x0)
  74. #define $STANDARD_INFORMATION (0x10)
  75. #define $ATTRIBUTE_LIST (0x20)
  76. #define $FILE_NAME (0x30)
  77. #define $VOLUME_VERSION (0x40)
  78. #define $OBJECT_ID (0x40) // starting in NT 5.0
  79. #define $SECURITY_DESCRIPTOR (0x50)
  80. #define $VOLUME_NAME (0x60)
  81. #define $VOLUME_INFORMATION (0x70)
  82. #define $DATA (0x80)
  83. #define $INDEX_ROOT (0x90)
  84. #define $INDEX_ALLOCATION (0xA0)
  85. #define $BITMAP (0xB0)
  86. #define $SYMBOLIC_LINK (0xC0)
  87. #define $REPARSE_POINT (0xC0) // starting in NT 5.0
  88. #define $EA_INFORMATION (0xD0)
  89. #define $EA_DATA (0xE0)
  90. #define $FIRST_USER_DEFINED_ATTRIBUTE_1 (0x100) // true up to NT 4.0
  91. #define $PROPERTY_SET (0xF0) // starting in NT 5.0
  92. #define $LOGGED_UTILITY_STREAM (0x100) // starting in NT 5.0
  93. #define $FIRST_USER_DEFINED_ATTRIBUTE_2 (0x1000) // starting in NT 5.0
  94. #define $END (0xFFFFFFFF)
  95. //
  96. // The boot sector is duplicated on the partition. The first copy is on
  97. // the first physical sector (LBN == 0) of the partition, and the second
  98. // copy is at <number sectors on partition> / 2. If the first copy can
  99. // not be read when trying to mount the disk, the second copy may be read
  100. // and has the identical contents. Format must figure out which cluster
  101. // the second boot record belongs in, and it must zero all of the other
  102. // sectors that happen to be in the same cluster. The boot file minimally
  103. // contains with two clusters, which are the two clusters which contain the
  104. // copies of the boot record. If format knows that some system likes to
  105. // put code somewhere, then it should also align this requirement to
  106. // even clusters, and add that to the boot file as well.
  107. //
  108. //
  109. // Define the boot sector. Note that MFT2 is exactly three file record
  110. // segments long, and it mirrors the first three file record segments from
  111. // the MFT, which are MFT, MFT2 and the Log File.
  112. //
  113. // The Oem field contains the ASCII characters "NTFS ".
  114. //
  115. // The Checksum field is a simple additive checksum of all of the ULONGs
  116. // which precede the Checksum ULONG. The rest of the sector is not included
  117. // in this Checksum.
  118. //
  119. typedef struct _PACKED_BOOT_SECTOR {
  120. UCHAR Jump[3]; // offset = 0x000
  121. UCHAR Oem[8]; // offset = 0x003
  122. OLD_PACKED_BIOS_PARAMETER_BLOCK PackedBpb; // offset = 0x00B
  123. UCHAR PhysicalDrive; // offset = 0x024
  124. UCHAR ReservedForBootCode; // offset = 0x025
  125. UCHAR Unused[2]; // offset = 0x026
  126. LARGE_INTEGER NumberSectors; // offset = 0x028
  127. LCN MftStartLcn; // offset = 0x030
  128. LCN Mft2StartLcn; // offset = 0x038
  129. CHAR ClustersPerFileRecordSegment; // offset = 0x040
  130. UCHAR Unused1[3]; // offset = 0x041
  131. CHAR DefaultClustersPerIndexAllocationBuffer; // offset = 0x044
  132. UCHAR Unused2[3]; // offset = 0x047
  133. LARGE_INTEGER SerialNumber; // offset = 0x048
  134. ULONG Checksum; // offset = 0x050
  135. UCHAR BootStrap[0x200-0x054]; // offset = 0x054
  136. } PACKED_BOOT_SECTOR; // sizeof = 0x200
  137. typedef PACKED_BOOT_SECTOR *PPACKED_BOOT_SECTOR;
  138. // Update sequence array structures--see ntos\inc\cache.h for
  139. // description.
  140. #define SEQUENCE_NUMBER_STRIDE (512)
  141. DEFINE_TYPE( USHORT, UPDATE_SEQUENCE_NUMBER );
  142. typedef struct _UNTFS_MULTI_SECTOR_HEADER {
  143. UCHAR Signature[4];
  144. USHORT UpdateSequenceArrayOffset; // byte offset
  145. USHORT UpdateSequenceArraySize; // number of Update Sequence Numbers
  146. };
  147. DEFINE_TYPE( _UNTFS_MULTI_SECTOR_HEADER, UNTFS_MULTI_SECTOR_HEADER );
  148. typedef UPDATE_SEQUENCE_NUMBER UPDATE_SEQUENCE_ARRAY[1];
  149. typedef UPDATE_SEQUENCE_ARRAY *PUPDATE_SEQUENCE_ARRAY;
  150. //
  151. // File Record Segment. This is the header that begins every File Record
  152. // Segment in the Master File Table.
  153. //
  154. typedef struct _FILE_RECORD_SEGMENT_HEADER {
  155. UNTFS_MULTI_SECTOR_HEADER MultiSectorHeader;
  156. LSN Lsn;
  157. USHORT SequenceNumber;
  158. USHORT ReferenceCount;
  159. USHORT FirstAttributeOffset;
  160. USHORT Flags; // FILE_xxx flags
  161. ULONG FirstFreeByte; // byte-offset
  162. ULONG BytesAvailable; // Size of FRS
  163. FILE_REFERENCE BaseFileRecordSegment;
  164. USHORT NextAttributeInstance; // Attribute instance tag for next insert.
  165. USHORT SegmentNumberHighPart; // used in mft recovery
  166. ULONG SegmentNumberLowPart; // used in mft recovery
  167. UPDATE_SEQUENCE_ARRAY UpdateArrayForCreateOnly;
  168. };
  169. typedef struct _FILE_RECORD_SEGMENT_HEADER_V0 {
  170. UNTFS_MULTI_SECTOR_HEADER MultiSectorHeader;
  171. LSN Lsn;
  172. USHORT SequenceNumber;
  173. USHORT ReferenceCount;
  174. USHORT FirstAttributeOffset;
  175. USHORT Flags; // FILE_xxx flags
  176. ULONG FirstFreeByte; // byte-offset
  177. ULONG BytesAvailable; // Size of FRS
  178. FILE_REFERENCE BaseFileRecordSegment;
  179. USHORT NextAttributeInstance; // Attribute instance tag for next insert.
  180. UPDATE_SEQUENCE_ARRAY UpdateArrayForCreateOnly;
  181. };
  182. DEFINE_TYPE( _FILE_RECORD_SEGMENT_HEADER, FILE_RECORD_SEGMENT_HEADER );
  183. DEFINE_TYPE( _FILE_RECORD_SEGMENT_HEADER_V0, FILE_RECORD_SEGMENT_HEADER_V0 );
  184. //
  185. // FILE_xxx flags.
  186. //
  187. #define FILE_RECORD_SEGMENT_IN_USE (0x0001)
  188. #define FILE_FILE_NAME_INDEX_PRESENT (0x0002)
  189. #define FILE_SYSTEM_FILE (0x0004)
  190. #define FILE_VIEW_INDEX_PRESENT (0x0008)
  191. //
  192. // Attribute Record. Logically an attribute has a type, an optional name,
  193. // and a value, however the storage details make it a little more complicated.
  194. // For starters, an attribute's value may either be resident in the file
  195. // record segment itself, on nonresident in a separate data stream. If it
  196. // is nonresident, it may actually exist multiple times in multiple file
  197. // record segments to describe different ranges of VCNs.
  198. //
  199. // Attribute Records are always aligned on a quad word (64-bit) boundary.
  200. //
  201. // Note that SIZE_OF_RESIDENT_HEADER and SIZE_OF_NONRESIDENT_HEADER
  202. // must correspond to the ATTRIBUTE_RECORD_HEADER structure.
  203. #define SIZE_OF_RESIDENT_HEADER 24
  204. #define SIZE_OF_NONRESIDENT_HEADER 64
  205. typedef struct _ATTRIBUTE_RECORD_HEADER {
  206. ATTRIBUTE_TYPE_CODE TypeCode;
  207. ULONG RecordLength;
  208. UCHAR FormCode;
  209. UCHAR NameLength; // length in characters
  210. USHORT NameOffset; // byte offset from start of record
  211. USHORT Flags; // ATTRIBUTE_xxx flags.
  212. USHORT Instance; // FRS-unique attribute instance tag
  213. union {
  214. //
  215. // Resident Form. Attribute resides in file record segment.
  216. //
  217. struct {
  218. ULONG ValueLength; // in bytes
  219. USHORT ValueOffset; // byte offset from start of record
  220. UCHAR ResidentFlags; // RESIDENT_FORM_xxx Flags.
  221. UCHAR Reserved;
  222. } Resident;
  223. //
  224. // Nonresident Form. Attribute resides in separate stream.
  225. //
  226. struct {
  227. VCN2 LowestVcn;
  228. VCN2 HighestVcn;
  229. USHORT MappingPairsOffset; // byte offset from start of record
  230. UCHAR CompressionUnit;
  231. UCHAR Reserved[5];
  232. LARGE_INTEGER AllocatedLength;
  233. LARGE_INTEGER FileSize;
  234. LARGE_INTEGER ValidDataLength;
  235. LARGE_INTEGER TotalAllocated;
  236. //
  237. // Mapping Pairs Array follows, starting at the offset given
  238. // above. See the extended comment in ntfs.h.
  239. //
  240. } Nonresident;
  241. } Form;
  242. };
  243. DEFINE_TYPE( _ATTRIBUTE_RECORD_HEADER, ATTRIBUTE_RECORD_HEADER );
  244. //
  245. // Attribute Form Codes
  246. //
  247. #define RESIDENT_FORM (0x00)
  248. #define NONRESIDENT_FORM (0x01)
  249. //
  250. // Define Attribute Flags
  251. //
  252. #define ATTRIBUTE_FLAG_COMPRESSION_MASK (0x00FF)
  253. #define ATTRIBUTE_FLAG_SPARSE (0x8000)
  254. #define ATTRIBUTE_FLAG_ENCRYPTED (0x4000)
  255. //
  256. // RESIDENT_FORM_xxx flags
  257. //
  258. //
  259. // This attribute is indexed.
  260. //
  261. #define RESIDENT_FORM_INDEXED (0x01)
  262. //
  263. // The maximum attribute name length is 255 (in chars)
  264. //
  265. #define NTFS_MAX_ATTR_NAME_LEN (255)
  266. //
  267. // Volume Information attribute. This attribute is only intended to be
  268. // used on the Volume DASD file.
  269. //
  270. typedef struct _VOLUME_INFORMATION {
  271. LARGE_INTEGER Reserved;
  272. //
  273. // Major and minor version number of NTFS on this volume, starting
  274. // with 1.0. The major and minor version numbers are set from the
  275. // major and minor version of the Format and NTFS implementation for
  276. // which they are initialized. The policy for incementing major and
  277. // minor versions will always be decided on a case by case basis, however,
  278. // the following two paragraphs attempt to suggest an approximate strategy.
  279. //
  280. // The major version number is incremented if/when a volume format
  281. // change is made which requires major structure changes (hopefully
  282. // never?). If an implementation of NTFS sees a volume with a higher
  283. // major version number, it should refuse to mount the volume. If a
  284. // newer implementation of NTFS sees an older major version number,
  285. // it knows the volume cannot be accessed without performing a one-time
  286. // conversion.
  287. //
  288. // The minor version number is incremented if/when minor enhancements
  289. // are made to a major version, which potentially support enhanced
  290. // functionality through additional file or attribute record fields,
  291. // or new system-defined files or attributes. If an older implementation
  292. // of NTFS sees a newer minor version number on a volume, it may issue
  293. // some kind of warning, but it will proceed to access the volume - with
  294. // presumably some degradation in functionality compared to the version
  295. // of NTFS which initialized the volume. If a newer implementation of
  296. // NTFS sees a volume with an older minor version number, it may issue
  297. // a warning and proceed. In this case, it may choose to increment the
  298. // minor version number on the volume and begin full or incremental
  299. // upgrade of the volume on an as-needed basis. It may also leave the
  300. // minor version number unchanged, until some sort of explicit directive
  301. // from the user specifies that the minor version should be updated.
  302. //
  303. UCHAR MajorVersion;
  304. UCHAR MinorVersion;
  305. //
  306. // VOLUME_xxx flags.
  307. //
  308. USHORT VolumeFlags;
  309. } VOLUME_INFORMATION;
  310. typedef VOLUME_INFORMATION *PVOLUME_INFORMATION;
  311. // Current version number:
  312. //
  313. #define NTFS_CURRENT_MAJOR_VERSION 1
  314. #define NTFS_CURRENT_MINOR_VERSION 2
  315. //
  316. // Volume is dirty
  317. //
  318. #define VOLUME_DIRTY (0x0001)
  319. #define VOLUME_RESIZE_LOG_FILE (0x0002)
  320. #define VOLUME_UPGRADE_ON_MOUNT (0x0004)
  321. #define VOLUME_MOUNTED_ON_40 (0x0008)
  322. #define VOLUME_DELETE_USN_UNDERWAY (0x0010)
  323. #define VOLUME_REPAIR_OBJECT_ID (0x0020)
  324. #define VOLUME_CHKDSK_RAN_ONCE (0x4000)
  325. #define VOLUME_CHKDSK_RAN (0x8000)
  326. #include <poppack.h>
  327. // Macros:
  328. #define QuadAlign( n ) \
  329. (((n) + 7) & ~7 )
  330. #define DwordAlign( n ) \
  331. (((n) + 3) & ~3 )
  332. #define IsQuadAligned( n ) \
  333. (((n) & 7) == 0)
  334. #define IsDwordAligned( n ) \
  335. (((n) & 3) == 0)
  336. #endif // _UNTFS2_DEFN_