• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * layout.h - Ntfs on-disk layout structures.  Originated from the Linux-NTFS project.
3  *
4  * Copyright (c) 2000-2005 Anton Altaparmakov
5  * Copyright (c)      2005 Yura Pakhuchiy
6  * Copyright (c) 2005-2006 Szabolcs Szakacsits
7  *
8  * This program/include file is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as published
10  * by the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program/include file is distributed in the hope that it will be
14  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program (in the main directory of the NTFS-3G
20  * distribution in the file COPYING); if not, write to the Free Software
21  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 
24 #ifndef _NTFS_LAYOUT_H
25 #define _NTFS_LAYOUT_H
26 
27 #include "types.h"
28 #include "endians.h"
29 #include "support.h"
30 
31 /* The NTFS oem_id */
32 #define magicNTFS	const_cpu_to_le64(0x202020205346544e)	/* "NTFS    " */
33 #define NTFS_SB_MAGIC	0x5346544e				/* 'NTFS' */
34 
35 /*
36  * Location of bootsector on partition:
37  *	The standard NTFS_BOOT_SECTOR is on sector 0 of the partition.
38  *	On NT4 and above there is one backup copy of the boot sector to
39  *	be found on the last sector of the partition (not normally accessible
40  *	from within Windows as the bootsector contained number of sectors
41  *	value is one less than the actual value!).
42  *	On versions of NT 3.51 and earlier, the backup copy was located at
43  *	number of sectors/2 (integer divide), i.e. in the middle of the volume.
44  */
45 
46 /**
47  * struct BIOS_PARAMETER_BLOCK - BIOS parameter block (bpb) structure.
48  */
49 typedef struct {
50 	le16 bytes_per_sector;		/* Size of a sector in bytes. */
51 	u8  sectors_per_cluster;	/* Size of a cluster in sectors. */
52 	le16 reserved_sectors;		/* zero */
53 	u8  fats;			/* zero */
54 	le16 root_entries;		/* zero */
55 	le16 sectors;			/* zero */
56 	u8  media_type;			/* 0xf8 = hard disk */
57 	le16 sectors_per_fat;		/* zero */
58 /*0x0d*/le16 sectors_per_track;		/* Required to boot Windows. */
59 /*0x0f*/le16 heads;			/* Required to boot Windows. */
60 /*0x11*/le32 hidden_sectors;		/* Offset to the start of the partition
61 					   relative to the disk in sectors.
62 					   Required to boot Windows. */
63 /*0x15*/le32 large_sectors;		/* zero */
64 /* sizeof() = 25 (0x19) bytes */
65 } __attribute__((__packed__)) BIOS_PARAMETER_BLOCK;
66 
67 /**
68  * struct NTFS_BOOT_SECTOR - NTFS boot sector structure.
69  */
70 typedef struct {
71 	u8  jump[3];			/* Irrelevant (jump to boot up code).*/
72 	le64 oem_id;			/* Magic "NTFS    ". */
73 /*0x0b*/BIOS_PARAMETER_BLOCK bpb;	/* See BIOS_PARAMETER_BLOCK. */
74 	u8 physical_drive;		/* 0x00 floppy, 0x80 hard disk */
75 	u8 current_head;		/* zero */
76 	u8 extended_boot_signature; 	/* 0x80 */
77 	u8 reserved2;			/* zero */
78 /*0x28*/sle64 number_of_sectors;		/* Number of sectors in volume. Gives
79 					   maximum volume size of 2^63 sectors.
80 					   Assuming standard sector size of 512
81 					   bytes, the maximum byte size is
82 					   approx. 4.7x10^21 bytes. (-; */
83 	sle64 mft_lcn;			/* Cluster location of mft data. */
84 	sle64 mftmirr_lcn;		/* Cluster location of copy of mft. */
85 	s8  clusters_per_mft_record;	/* Mft record size in clusters. */
86 	u8  reserved0[3];		/* zero */
87 	s8  clusters_per_index_record;	/* Index block size in clusters. */
88 	u8  reserved1[3];		/* zero */
89 	le64 volume_serial_number;	/* Irrelevant (serial number). */
90 	le32 checksum;			/* Boot sector checksum. */
91 /*0x54*/u8  bootstrap[426];		/* Irrelevant (boot up code). */
92 	le16 end_of_sector_marker;	/* End of bootsector magic. Always is
93 					   0xaa55 in little endian. */
94 /* sizeof() = 512 (0x200) bytes */
95 } __attribute__((__packed__)) NTFS_BOOT_SECTOR;
96 
97 /**
98  * enum NTFS_RECORD_TYPES -
99  *
100  * Magic identifiers present at the beginning of all ntfs record containing
101  * records (like mft records for example).
102  */
103 typedef enum {
104 	/* Found in $MFT/$DATA. */
105 	magic_FILE = const_cpu_to_le32(0x454c4946), /* Mft entry. */
106 	magic_INDX = const_cpu_to_le32(0x58444e49), /* Index buffer. */
107 	magic_HOLE = const_cpu_to_le32(0x454c4f48), /* ? (NTFS 3.0+?) */
108 
109 	/* Found in $LogFile/$DATA. */
110 	magic_RSTR = const_cpu_to_le32(0x52545352), /* Restart page. */
111 	magic_RCRD = const_cpu_to_le32(0x44524352), /* Log record page. */
112 
113 	/* Found in $LogFile/$DATA.  (May be found in $MFT/$DATA, also?) */
114 	magic_CHKD = const_cpu_to_le32(0x444b4843), /* Modified by chkdsk. */
115 
116 	/* Found in all ntfs record containing records. */
117 	magic_BAAD = const_cpu_to_le32(0x44414142), /* Failed multi sector
118 						       transfer was detected. */
119 
120 	/*
121 	 * Found in $LogFile/$DATA when a page is full or 0xff bytes and is
122 	 * thus not initialized.  User has to initialize the page before using
123 	 * it.
124 	 */
125 	magic_empty = const_cpu_to_le32(0xffffffff),/* Record is empty and has
126 						       to be initialized before
127 						       it can be used. */
128 } NTFS_RECORD_TYPES;
129 
130 /*
131  * Generic magic comparison macros. Finally found a use for the ## preprocessor
132  * operator! (-8
133  */
134 #define ntfs_is_magic(x, m)	(   (u32)(x) == (u32)magic_##m )
135 #define ntfs_is_magicp(p, m)	( *(u32*)(p) == (u32)magic_##m )
136 
137 /*
138  * Specialised magic comparison macros for the NTFS_RECORD_TYPES defined above.
139  */
140 #define ntfs_is_file_record(x)	( ntfs_is_magic (x, FILE) )
141 #define ntfs_is_file_recordp(p)	( ntfs_is_magicp(p, FILE) )
142 #define ntfs_is_mft_record(x)	( ntfs_is_file_record(x) )
143 #define ntfs_is_mft_recordp(p)	( ntfs_is_file_recordp(p) )
144 #define ntfs_is_indx_record(x)	( ntfs_is_magic (x, INDX) )
145 #define ntfs_is_indx_recordp(p)	( ntfs_is_magicp(p, INDX) )
146 #define ntfs_is_hole_record(x)	( ntfs_is_magic (x, HOLE) )
147 #define ntfs_is_hole_recordp(p)	( ntfs_is_magicp(p, HOLE) )
148 
149 #define ntfs_is_rstr_record(x)	( ntfs_is_magic (x, RSTR) )
150 #define ntfs_is_rstr_recordp(p)	( ntfs_is_magicp(p, RSTR) )
151 #define ntfs_is_rcrd_record(x)	( ntfs_is_magic (x, RCRD) )
152 #define ntfs_is_rcrd_recordp(p)	( ntfs_is_magicp(p, RCRD) )
153 
154 #define ntfs_is_chkd_record(x)	( ntfs_is_magic (x, CHKD) )
155 #define ntfs_is_chkd_recordp(p)	( ntfs_is_magicp(p, CHKD) )
156 
157 #define ntfs_is_baad_record(x)	( ntfs_is_magic (x, BAAD) )
158 #define ntfs_is_baad_recordp(p)	( ntfs_is_magicp(p, BAAD) )
159 
160 #define ntfs_is_empty_record(x)		( ntfs_is_magic (x, empty) )
161 #define ntfs_is_empty_recordp(p)	( ntfs_is_magicp(p, empty) )
162 
163 
164 /*
165  * The size of a logical sector in bytes, used as the sequence number stride for
166  * multi-sector transfers.  This is intended to be less than or equal to the
167  * physical sector size, since if this were greater than the physical sector
168  * size, then incomplete multi-sector transfers may not be detected.
169  */
170 #define NTFS_BLOCK_SIZE		512
171 #define NTFS_BLOCK_SIZE_BITS	9
172 
173 /**
174  * struct NTFS_RECORD -
175  *
176  * The Update Sequence Array (usa) is an array of the le16 values which belong
177  * to the end of each sector protected by the update sequence record in which
178  * this array is contained. Note that the first entry is the Update Sequence
179  * Number (usn), a cyclic counter of how many times the protected record has
180  * been written to disk. The values 0 and -1 (ie. 0xffff) are not used. All
181  * last le16's of each sector have to be equal to the usn (during reading) or
182  * are set to it (during writing). If they are not, an incomplete multi sector
183  * transfer has occurred when the data was written.
184  * The maximum size for the update sequence array is fixed to:
185  *	maximum size = usa_ofs + (usa_count * 2) = 510 bytes
186  * The 510 bytes comes from the fact that the last le16 in the array has to
187  * (obviously) finish before the last le16 of the first 512-byte sector.
188  * This formula can be used as a consistency check in that usa_ofs +
189  * (usa_count * 2) has to be less than or equal to 510.
190  */
191 typedef struct {
192 	NTFS_RECORD_TYPES magic;/* A four-byte magic identifying the
193 				   record type and/or status. */
194 	le16 usa_ofs;		/* Offset to the Update Sequence Array (usa)
195 				   from the start of the ntfs record. */
196 	le16 usa_count;		/* Number of le16 sized entries in the usa
197 				   including the Update Sequence Number (usn),
198 				   thus the number of fixups is the usa_count
199 				   minus 1. */
200 } __attribute__((__packed__)) NTFS_RECORD;
201 
202 /**
203  * enum NTFS_SYSTEM_FILES - System files mft record numbers.
204  *
205  * All these files are always marked as used in the bitmap attribute of the
206  * mft; presumably in order to avoid accidental allocation for random other
207  * mft records. Also, the sequence number for each of the system files is
208  * always equal to their mft record number and it is never modified.
209  */
210 typedef enum {
211 	FILE_MFT	= 0,	/* Master file table (mft). Data attribute
212 				   contains the entries and bitmap attribute
213 				   records which ones are in use (bit==1). */
214 	FILE_MFTMirr	= 1,	/* Mft mirror: copy of first four mft records
215 				   in data attribute. If cluster size > 4kiB,
216 				   copy of first N mft records, with
217 					N = cluster_size / mft_record_size. */
218 	FILE_LogFile	= 2,	/* Journalling log in data attribute. */
219 	FILE_Volume	= 3,	/* Volume name attribute and volume information
220 				   attribute (flags and ntfs version). Windows
221 				   refers to this file as volume DASD (Direct
222 				   Access Storage Device). */
223 	FILE_AttrDef	= 4,	/* Array of attribute definitions in data
224 				   attribute. */
225 	FILE_root	= 5,	/* Root directory. */
226 	FILE_Bitmap	= 6,	/* Allocation bitmap of all clusters (lcns) in
227 				   data attribute. */
228 	FILE_Boot	= 7,	/* Boot sector (always at cluster 0) in data
229 				   attribute. */
230 	FILE_BadClus	= 8,	/* Contains all bad clusters in the non-resident
231 				   data attribute. */
232 	FILE_Secure	= 9,	/* Shared security descriptors in data attribute
233 				   and two indexes into the descriptors.
234 				   Appeared in Windows 2000. Before that, this
235 				   file was named $Quota but was unused. */
236 	FILE_UpCase	= 10,	/* Uppercase equivalents of all 65536 Unicode
237 				   characters in data attribute. */
238 	FILE_Extend	= 11,	/* Directory containing other system files (eg.
239 				   $ObjId, $Quota, $Reparse and $UsnJrnl). This
240 				   is new to NTFS3.0. */
241 	FILE_reserved12	= 12,	/* Reserved for future use (records 12-15). */
242 	FILE_reserved13	= 13,
243 	FILE_reserved14	= 14,
244 	FILE_mft_data	= 15,	/* Reserved for first extent of $MFT:$DATA */
245 	FILE_first_user	= 16,	/* First user file, used as test limit for
246 				   whether to allow opening a file or not. */
247 } NTFS_SYSTEM_FILES;
248 
249 /**
250  * enum MFT_RECORD_FLAGS -
251  *
252  * These are the so far known MFT_RECORD_* flags (16-bit) which contain
253  * information about the mft record in which they are present.
254  *
255  * MFT_RECORD_IS_4 exists on all $Extend sub-files.
256  * It seems that it marks it is a metadata file with MFT record >24, however,
257  * it is unknown if it is limited to metadata files only.
258  *
259  * MFT_RECORD_IS_VIEW_INDEX exists on every metafile with a non directory
260  * index, that means an INDEX_ROOT and an INDEX_ALLOCATION with a name other
261  * than "$I30". It is unknown if it is limited to metadata files only.
262  */
263 typedef enum {
264 	MFT_RECORD_IN_USE		= const_cpu_to_le16(0x0001),
265 	MFT_RECORD_IS_DIRECTORY		= const_cpu_to_le16(0x0002),
266 	MFT_RECORD_IS_4			= const_cpu_to_le16(0x0004),
267 	MFT_RECORD_IS_VIEW_INDEX	= const_cpu_to_le16(0x0008),
268 	MFT_REC_SPACE_FILLER		= 0xffff, /* Just to make flags
269 						     16-bit. */
270 } __attribute__((__packed__)) MFT_RECORD_FLAGS;
271 
272 /*
273  * mft references (aka file references or file record segment references) are
274  * used whenever a structure needs to refer to a record in the mft.
275  *
276  * A reference consists of a 48-bit index into the mft and a 16-bit sequence
277  * number used to detect stale references.
278  *
279  * For error reporting purposes we treat the 48-bit index as a signed quantity.
280  *
281  * The sequence number is a circular counter (skipping 0) describing how many
282  * times the referenced mft record has been (re)used. This has to match the
283  * sequence number of the mft record being referenced, otherwise the reference
284  * is considered stale and removed (FIXME: only ntfsck or the driver itself?).
285  *
286  * If the sequence number is zero it is assumed that no sequence number
287  * consistency checking should be performed.
288  *
289  * FIXME: Since inodes are 32-bit as of now, the driver needs to always check
290  * for high_part being 0 and if not either BUG(), cause a panic() or handle
291  * the situation in some other way. This shouldn't be a problem as a volume has
292  * to become HUGE in order to need more than 32-bits worth of mft records.
293  * Assuming the standard mft record size of 1kb only the records (never mind
294  * the non-resident attributes, etc.) would require 4Tb of space on their own
295  * for the first 32 bits worth of records. This is only if some strange person
296  * doesn't decide to foul play and make the mft sparse which would be a really
297  * horrible thing to do as it would trash our current driver implementation. )-:
298  * Do I hear screams "we want 64-bit inodes!" ?!? (-;
299  *
300  * FIXME: The mft zone is defined as the first 12% of the volume. This space is
301  * reserved so that the mft can grow contiguously and hence doesn't become
302  * fragmented. Volume free space includes the empty part of the mft zone and
303  * when the volume's free 88% are used up, the mft zone is shrunk by a factor
304  * of 2, thus making more space available for more files/data. This process is
305  * repeated every time there is no more free space except for the mft zone until
306  * there really is no more free space.
307  */
308 
309 /*
310  * Typedef the MFT_REF as a 64-bit value for easier handling.
311  * Also define two unpacking macros to get to the reference (MREF) and
312  * sequence number (MSEQNO) respectively.
313  * The _LE versions are to be applied on little endian MFT_REFs.
314  * Note: The _LE versions will return a CPU endian formatted value!
315  */
316 #define MFT_REF_MASK_CPU 0x0000ffffffffffffULL
317 #define MFT_REF_MASK_LE const_cpu_to_le64(MFT_REF_MASK_CPU)
318 
319 typedef u64 MFT_REF;
320 typedef le64 leMFT_REF;   /* a little-endian MFT_MREF */
321 
322 #define MK_MREF(m, s)	((MFT_REF)(((MFT_REF)(s) << 48) |		\
323 					((MFT_REF)(m) & MFT_REF_MASK_CPU)))
324 #define MK_LE_MREF(m, s) const_cpu_to_le64(((MFT_REF)(((MFT_REF)(s) << 48) | \
325 					((MFT_REF)(m) & MFT_REF_MASK_CPU))))
326 
327 #define MREF(x)		((u64)((x) & MFT_REF_MASK_CPU))
328 #define MSEQNO(x)	((u16)(((x) >> 48) & 0xffff))
329 #define MREF_LE(x)	((u64)(const_le64_to_cpu(x) & MFT_REF_MASK_CPU))
330 #define MSEQNO_LE(x)	((u16)((const_le64_to_cpu(x) >> 48) & 0xffff))
331 
332 #define IS_ERR_MREF(x)	(((x) & 0x0000800000000000ULL) ? 1 : 0)
333 #define ERR_MREF(x)	((u64)((s64)(x)))
334 #define MREF_ERR(x)	((int)((s64)(x)))
335 
336 /**
337  * struct MFT_RECORD - An MFT record layout (NTFS 3.1+)
338  *
339  * The mft record header present at the beginning of every record in the mft.
340  * This is followed by a sequence of variable length attribute records which
341  * is terminated by an attribute of type AT_END which is a truncated attribute
342  * in that it only consists of the attribute type code AT_END and none of the
343  * other members of the attribute structure are present.
344  */
345 typedef struct {
346 /*Ofs*/
347 /*  0	NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */
348 	NTFS_RECORD_TYPES magic;/* Usually the magic is "FILE". */
349 	le16 usa_ofs;		/* See NTFS_RECORD definition above. */
350 	le16 usa_count;		/* See NTFS_RECORD definition above. */
351 
352 /*  8*/	leLSN lsn;		/* $LogFile sequence number for this record.
353 				   Changed every time the record is modified. */
354 /* 16*/	le16 sequence_number;	/* Number of times this mft record has been
355 				   reused. (See description for MFT_REF
356 				   above.) NOTE: The increment (skipping zero)
357 				   is done when the file is deleted. NOTE: If
358 				   this is zero it is left zero. */
359 /* 18*/	le16 link_count;		/* Number of hard links, i.e. the number of
360 				   directory entries referencing this record.
361 				   NOTE: Only used in mft base records.
362 				   NOTE: When deleting a directory entry we
363 				   check the link_count and if it is 1 we
364 				   delete the file. Otherwise we delete the
365 				   FILE_NAME_ATTR being referenced by the
366 				   directory entry from the mft record and
367 				   decrement the link_count.
368 				   FIXME: Careful with Win32 + DOS names! */
369 /* 20*/	le16 attrs_offset;	/* Byte offset to the first attribute in this
370 				   mft record from the start of the mft record.
371 				   NOTE: Must be aligned to 8-byte boundary. */
372 /* 22*/	MFT_RECORD_FLAGS flags;	/* Bit array of MFT_RECORD_FLAGS. When a file
373 				   is deleted, the MFT_RECORD_IN_USE flag is
374 				   set to zero. */
375 /* 24*/	le32 bytes_in_use;	/* Number of bytes used in this mft record.
376 				   NOTE: Must be aligned to 8-byte boundary. */
377 /* 28*/	le32 bytes_allocated;	/* Number of bytes allocated for this mft
378 				   record. This should be equal to the mft
379 				   record size. */
380 /* 32*/	leMFT_REF base_mft_record;
381 				/* This is zero for base mft records.
382 				   When it is not zero it is a mft reference
383 				   pointing to the base mft record to which
384 				   this record belongs (this is then used to
385 				   locate the attribute list attribute present
386 				   in the base record which describes this
387 				   extension record and hence might need
388 				   modification when the extension record
389 				   itself is modified, also locating the
390 				   attribute list also means finding the other
391 				   potential extents, belonging to the non-base
392 				   mft record). */
393 /* 40*/	le16 next_attr_instance; /* The instance number that will be
394 				   assigned to the next attribute added to this
395 				   mft record. NOTE: Incremented each time
396 				   after it is used. NOTE: Every time the mft
397 				   record is reused this number is set to zero.
398 				   NOTE: The first instance number is always 0.
399 				 */
400 /* The below fields are specific to NTFS 3.1+ (Windows XP and above): */
401 /* 42*/ le16 reserved;		/* Reserved/alignment. */
402 /* 44*/ le32 mft_record_number;	/* Number of this mft record. */
403 /* sizeof() = 48 bytes */
404 /*
405  * When (re)using the mft record, we place the update sequence array at this
406  * offset, i.e. before we start with the attributes. This also makes sense,
407  * otherwise we could run into problems with the update sequence array
408  * containing in itself the last two bytes of a sector which would mean that
409  * multi sector transfer protection wouldn't work. As you can't protect data
410  * by overwriting it since you then can't get it back...
411  * When reading we obviously use the data from the ntfs record header.
412  */
413 } __attribute__((__packed__)) MFT_RECORD;
414 
415 /**
416  * struct MFT_RECORD_OLD - An MFT record layout (NTFS <=3.0)
417  *
418  * This is the version without the NTFS 3.1+ specific fields.
419  */
420 typedef struct {
421 /*Ofs*/
422 /*  0	NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */
423 	NTFS_RECORD_TYPES magic;/* Usually the magic is "FILE". */
424 	le16 usa_ofs;		/* See NTFS_RECORD definition above. */
425 	le16 usa_count;		/* See NTFS_RECORD definition above. */
426 
427 /*  8*/	leLSN lsn;		/* $LogFile sequence number for this record.
428 				   Changed every time the record is modified. */
429 /* 16*/	le16 sequence_number;	/* Number of times this mft record has been
430 				   reused. (See description for MFT_REF
431 				   above.) NOTE: The increment (skipping zero)
432 				   is done when the file is deleted. NOTE: If
433 				   this is zero it is left zero. */
434 /* 18*/	le16 link_count;		/* Number of hard links, i.e. the number of
435 				   directory entries referencing this record.
436 				   NOTE: Only used in mft base records.
437 				   NOTE: When deleting a directory entry we
438 				   check the link_count and if it is 1 we
439 				   delete the file. Otherwise we delete the
440 				   FILE_NAME_ATTR being referenced by the
441 				   directory entry from the mft record and
442 				   decrement the link_count.
443 				   FIXME: Careful with Win32 + DOS names! */
444 /* 20*/	le16 attrs_offset;	/* Byte offset to the first attribute in this
445 				   mft record from the start of the mft record.
446 				   NOTE: Must be aligned to 8-byte boundary. */
447 /* 22*/	MFT_RECORD_FLAGS flags;	/* Bit array of MFT_RECORD_FLAGS. When a file
448 				   is deleted, the MFT_RECORD_IN_USE flag is
449 				   set to zero. */
450 /* 24*/	le32 bytes_in_use;	/* Number of bytes used in this mft record.
451 				   NOTE: Must be aligned to 8-byte boundary. */
452 /* 28*/	le32 bytes_allocated;	/* Number of bytes allocated for this mft
453 				   record. This should be equal to the mft
454 				   record size. */
455 /* 32*/	leMFT_REF base_mft_record;
456 				/* This is zero for base mft records.
457 				   When it is not zero it is a mft reference
458 				   pointing to the base mft record to which
459 				   this record belongs (this is then used to
460 				   locate the attribute list attribute present
461 				   in the base record which describes this
462 				   extension record and hence might need
463 				   modification when the extension record
464 				   itself is modified, also locating the
465 				   attribute list also means finding the other
466 				   potential extents, belonging to the non-base
467 				   mft record). */
468 /* 40*/	le16 next_attr_instance; /* The instance number that will be
469 				   assigned to the next attribute added to this
470 				   mft record. NOTE: Incremented each time
471 				   after it is used. NOTE: Every time the mft
472 				   record is reused this number is set to zero.
473 				   NOTE: The first instance number is always 0.
474 				 */
475 /* sizeof() = 42 bytes */
476 /*
477  * When (re)using the mft record, we place the update sequence array at this
478  * offset, i.e. before we start with the attributes. This also makes sense,
479  * otherwise we could run into problems with the update sequence array
480  * containing in itself the last two bytes of a sector which would mean that
481  * multi sector transfer protection wouldn't work. As you can't protect data
482  * by overwriting it since you then can't get it back...
483  * When reading we obviously use the data from the ntfs record header.
484  */
485 } __attribute__((__packed__)) MFT_RECORD_OLD;
486 
487 /**
488  * enum ATTR_TYPES - System defined attributes (32-bit).
489  *
490  * Each attribute type has a corresponding attribute name (Unicode string of
491  * maximum 64 character length) as described by the attribute definitions
492  * present in the data attribute of the $AttrDef system file.
493  *
494  * On NTFS 3.0 volumes the names are just as the types are named in the below
495  * enum exchanging AT_ for the dollar sign ($). If that isn't a revealing
496  * choice of symbol... (-;
497  */
498 typedef enum {
499 	AT_UNUSED			= const_cpu_to_le32(         0),
500 	AT_STANDARD_INFORMATION		= const_cpu_to_le32(      0x10),
501 	AT_ATTRIBUTE_LIST		= const_cpu_to_le32(      0x20),
502 	AT_FILE_NAME			= const_cpu_to_le32(      0x30),
503 	AT_OBJECT_ID			= const_cpu_to_le32(      0x40),
504 	AT_SECURITY_DESCRIPTOR		= const_cpu_to_le32(      0x50),
505 	AT_VOLUME_NAME			= const_cpu_to_le32(      0x60),
506 	AT_VOLUME_INFORMATION		= const_cpu_to_le32(      0x70),
507 	AT_DATA				= const_cpu_to_le32(      0x80),
508 	AT_INDEX_ROOT			= const_cpu_to_le32(      0x90),
509 	AT_INDEX_ALLOCATION		= const_cpu_to_le32(      0xa0),
510 	AT_BITMAP			= const_cpu_to_le32(      0xb0),
511 	AT_REPARSE_POINT		= const_cpu_to_le32(      0xc0),
512 	AT_EA_INFORMATION		= const_cpu_to_le32(      0xd0),
513 	AT_EA				= const_cpu_to_le32(      0xe0),
514 	AT_PROPERTY_SET			= const_cpu_to_le32(      0xf0),
515 	AT_LOGGED_UTILITY_STREAM	= const_cpu_to_le32(     0x100),
516 	AT_FIRST_USER_DEFINED_ATTRIBUTE	= const_cpu_to_le32(    0x1000),
517 	AT_END				= const_cpu_to_le32(0xffffffff),
518 } ATTR_TYPES;
519 
520 /**
521  * enum COLLATION_RULES - The collation rules for sorting views/indexes/etc
522  * (32-bit).
523  *
524  * COLLATION_BINARY - Collate by binary compare where the first byte is most
525  *	significant.
526  * COLLATION_FILE_NAME - Collate Unicode strings by comparing their 16-bit
527  *	coding units, primarily ignoring case using the volume's $UpCase table,
528  *	but falling back to a case-sensitive comparison if the names are equal
529  *	ignoring case.
530  * COLLATION_UNICODE_STRING - TODO: this is not yet implemented and still needs
531  *	to be properly documented --- is it really the same as
532  *	COLLATION_FILE_NAME?
533  * COLLATION_NTOFS_ULONG - Sorting is done according to ascending le32 key
534  *	values. E.g. used for $SII index in FILE_Secure, which sorts by
535  *	security_id (le32).
536  * COLLATION_NTOFS_SID - Sorting is done according to ascending SID values.
537  *	E.g. used for $O index in FILE_Extend/$Quota.
538  * COLLATION_NTOFS_SECURITY_HASH - Sorting is done first by ascending hash
539  *	values and second by ascending security_id values. E.g. used for $SDH
540  *	index in FILE_Secure.
541  * COLLATION_NTOFS_ULONGS - Sorting is done according to a sequence of ascending
542  *	le32 key values. E.g. used for $O index in FILE_Extend/$ObjId, which
543  *	sorts by object_id (16-byte), by splitting up the object_id in four
544  *	le32 values and using them as individual keys. E.g. take the following
545  *	two security_ids, stored as follows on disk:
546  *		1st: a1 61 65 b7 65 7b d4 11 9e 3d 00 e0 81 10 42 59
547  *		2nd: 38 14 37 d2 d2 f3 d4 11 a5 21 c8 6b 79 b1 97 45
548  *	To compare them, they are split into four le32 values each, like so:
549  *		1st: 0xb76561a1 0x11d47b65 0xe0003d9e 0x59421081
550  *		2nd: 0xd2371438 0x11d4f3d2 0x6bc821a5 0x4597b179
551  *	Now, it is apparent why the 2nd object_id collates after the 1st: the
552  *	first le32 value of the 1st object_id is less than the first le32 of
553  *	the 2nd object_id. If the first le32 values of both object_ids were
554  *	equal then the second le32 values would be compared, etc.
555  */
556 typedef enum {
557 	COLLATION_BINARY		= const_cpu_to_le32(0),
558 	COLLATION_FILE_NAME		= const_cpu_to_le32(1),
559 	COLLATION_UNICODE_STRING	= const_cpu_to_le32(2),
560 	COLLATION_NTOFS_ULONG		= const_cpu_to_le32(16),
561 	COLLATION_NTOFS_SID		= const_cpu_to_le32(17),
562 	COLLATION_NTOFS_SECURITY_HASH	= const_cpu_to_le32(18),
563 	COLLATION_NTOFS_ULONGS		= const_cpu_to_le32(19),
564 } COLLATION_RULES;
565 
566 /**
567  * enum ATTR_DEF_FLAGS -
568  *
569  * The flags (32-bit) describing attribute properties in the attribute
570  * definition structure.  FIXME: This information is based on Regis's
571  * information and, according to him, it is not certain and probably
572  * incomplete.  The INDEXABLE flag is fairly certainly correct as only the file
573  * name attribute has this flag set and this is the only attribute indexed in
574  * NT4.
575  */
576 typedef enum {
577 	ATTR_DEF_INDEXABLE	= const_cpu_to_le32(0x02), /* Attribute can be
578 					indexed. */
579 	ATTR_DEF_MULTIPLE	= const_cpu_to_le32(0x04), /* Attribute type
580 					can be present multiple times in the
581 					mft records of an inode. */
582 	ATTR_DEF_NOT_ZERO	= const_cpu_to_le32(0x08), /* Attribute value
583 					must contain at least one non-zero
584 					byte. */
585 	ATTR_DEF_INDEXED_UNIQUE	= const_cpu_to_le32(0x10), /* Attribute must be
586 					indexed and the attribute value must be
587 					unique for the attribute type in all of
588 					the mft records of an inode. */
589 	ATTR_DEF_NAMED_UNIQUE	= const_cpu_to_le32(0x20), /* Attribute must be
590 					named and the name must be unique for
591 					the attribute type in all of the mft
592 					records of an inode. */
593 	ATTR_DEF_RESIDENT	= const_cpu_to_le32(0x40), /* Attribute must be
594 					resident. */
595 	ATTR_DEF_ALWAYS_LOG	= const_cpu_to_le32(0x80), /* Always log
596 					modifications to this attribute,
597 					regardless of whether it is resident or
598 					non-resident.  Without this, only log
599 					modifications if the attribute is
600 					resident. */
601 } ATTR_DEF_FLAGS;
602 
603 /**
604  * struct ATTR_DEF -
605  *
606  * The data attribute of FILE_AttrDef contains a sequence of attribute
607  * definitions for the NTFS volume. With this, it is supposed to be safe for an
608  * older NTFS driver to mount a volume containing a newer NTFS version without
609  * damaging it (that's the theory. In practice it's: not damaging it too much).
610  * Entries are sorted by attribute type. The flags describe whether the
611  * attribute can be resident/non-resident and possibly other things, but the
612  * actual bits are unknown.
613  */
614 typedef struct {
615 /*hex ofs*/
616 /*  0*/	ntfschar name[0x40];		/* Unicode name of the attribute. Zero
617 					   terminated. */
618 /* 80*/	ATTR_TYPES type;		/* Type of the attribute. */
619 /* 84*/	le32 display_rule;		/* Default display rule.
620 					   FIXME: What does it mean? (AIA) */
621 /* 88*/ COLLATION_RULES collation_rule;	/* Default collation rule. */
622 /* 8c*/	ATTR_DEF_FLAGS flags;		/* Flags describing the attribute. */
623 /* 90*/	sle64 min_size;			/* Optional minimum attribute size. */
624 /* 98*/	sle64 max_size;			/* Maximum size of attribute. */
625 /* sizeof() = 0xa0 or 160 bytes */
626 } __attribute__((__packed__)) ATTR_DEF;
627 
628 /**
629  * enum ATTR_FLAGS - Attribute flags (16-bit).
630  */
631 typedef enum {
632 	ATTR_IS_COMPRESSED	= const_cpu_to_le16(0x0001),
633 	ATTR_COMPRESSION_MASK	= const_cpu_to_le16(0x00ff),  /* Compression
634 						method mask. Also, first
635 						illegal value. */
636 	ATTR_IS_ENCRYPTED	= const_cpu_to_le16(0x4000),
637 	ATTR_IS_SPARSE		= const_cpu_to_le16(0x8000),
638 } __attribute__((__packed__)) ATTR_FLAGS;
639 
640 /*
641  * Attribute compression.
642  *
643  * Only the data attribute is ever compressed in the current ntfs driver in
644  * Windows. Further, compression is only applied when the data attribute is
645  * non-resident. Finally, to use compression, the maximum allowed cluster size
646  * on a volume is 4kib.
647  *
648  * The compression method is based on independently compressing blocks of X
649  * clusters, where X is determined from the compression_unit value found in the
650  * non-resident attribute record header (more precisely: X = 2^compression_unit
651  * clusters). On Windows NT/2k, X always is 16 clusters (compression_unit = 4).
652  *
653  * There are three different cases of how a compression block of X clusters
654  * can be stored:
655  *
656  *   1) The data in the block is all zero (a sparse block):
657  *	  This is stored as a sparse block in the runlist, i.e. the runlist
658  *	  entry has length = X and lcn = -1. The mapping pairs array actually
659  *	  uses a delta_lcn value length of 0, i.e. delta_lcn is not present at
660  *	  all, which is then interpreted by the driver as lcn = -1.
661  *	  NOTE: Even uncompressed files can be sparse on NTFS 3.0 volumes, then
662  *	  the same principles apply as above, except that the length is not
663  *	  restricted to being any particular value.
664  *
665  *   2) The data in the block is not compressed:
666  *	  This happens when compression doesn't reduce the size of the block
667  *	  in clusters. I.e. if compression has a small effect so that the
668  *	  compressed data still occupies X clusters, then the uncompressed data
669  *	  is stored in the block.
670  *	  This case is recognised by the fact that the runlist entry has
671  *	  length = X and lcn >= 0. The mapping pairs array stores this as
672  *	  normal with a run length of X and some specific delta_lcn, i.e.
673  *	  delta_lcn has to be present.
674  *
675  *   3) The data in the block is compressed:
676  *	  The common case. This case is recognised by the fact that the run
677  *	  list entry has length L < X and lcn >= 0. The mapping pairs array
678  *	  stores this as normal with a run length of X and some specific
679  *	  delta_lcn, i.e. delta_lcn has to be present. This runlist entry is
680  *	  immediately followed by a sparse entry with length = X - L and
681  *	  lcn = -1. The latter entry is to make up the vcn counting to the
682  *	  full compression block size X.
683  *
684  * In fact, life is more complicated because adjacent entries of the same type
685  * can be coalesced. This means that one has to keep track of the number of
686  * clusters handled and work on a basis of X clusters at a time being one
687  * block. An example: if length L > X this means that this particular runlist
688  * entry contains a block of length X and part of one or more blocks of length
689  * L - X. Another example: if length L < X, this does not necessarily mean that
690  * the block is compressed as it might be that the lcn changes inside the block
691  * and hence the following runlist entry describes the continuation of the
692  * potentially compressed block. The block would be compressed if the
693  * following runlist entry describes at least X - L sparse clusters, thus
694  * making up the compression block length as described in point 3 above. (Of
695  * course, there can be several runlist entries with small lengths so that the
696  * sparse entry does not follow the first data containing entry with
697  * length < X.)
698  *
699  * NOTE: At the end of the compressed attribute value, there most likely is not
700  * just the right amount of data to make up a compression block, thus this data
701  * is not even attempted to be compressed. It is just stored as is, unless
702  * the number of clusters it occupies is reduced when compressed in which case
703  * it is stored as a compressed compression block, complete with sparse
704  * clusters at the end.
705  */
706 
707 /**
708  * enum RESIDENT_ATTR_FLAGS - Flags of resident attributes (8-bit).
709  */
710 typedef enum {
711 	RESIDENT_ATTR_IS_INDEXED = 0x01, /* Attribute is referenced in an index
712 					    (has implications for deleting and
713 					    modifying the attribute). */
714 } __attribute__((__packed__)) RESIDENT_ATTR_FLAGS;
715 
716 /**
717  * struct ATTR_RECORD - Attribute record header.
718  *
719  * Always aligned to 8-byte boundary.
720  */
721 typedef struct {
722 /*Ofs*/
723 /*  0*/	ATTR_TYPES type;	/* The (32-bit) type of the attribute. */
724 /*  4*/	le32 length;		/* Byte size of the resident part of the
725 				   attribute (aligned to 8-byte boundary).
726 				   Used to get to the next attribute. */
727 /*  8*/	u8 non_resident;	/* If 0, attribute is resident.
728 				   If 1, attribute is non-resident. */
729 /*  9*/	u8 name_length;		/* Unicode character size of name of attribute.
730 				   0 if unnamed. */
731 /* 10*/	le16 name_offset;	/* If name_length != 0, the byte offset to the
732 				   beginning of the name from the attribute
733 				   record. Note that the name is stored as a
734 				   Unicode string. When creating, place offset
735 				   just at the end of the record header. Then,
736 				   follow with attribute value or mapping pairs
737 				   array, resident and non-resident attributes
738 				   respectively, aligning to an 8-byte
739 				   boundary. */
740 /* 12*/	ATTR_FLAGS flags;	/* Flags describing the attribute. */
741 /* 14*/	le16 instance;		/* The instance of this attribute record. This
742 				   number is unique within this mft record (see
743 				   MFT_RECORD/next_attribute_instance notes
744 				   above for more details). */
745 /* 16*/	union {
746 		/* Resident attributes. */
747 		struct {
748 /* 16 */		le32 value_length; /* Byte size of attribute value. */
749 /* 20 */		le16 value_offset; /* Byte offset of the attribute
750 					       value from the start of the
751 					       attribute record. When creating,
752 					       align to 8-byte boundary if we
753 					       have a name present as this might
754 					       not have a length of a multiple
755 					       of 8-bytes. */
756 /* 22 */		RESIDENT_ATTR_FLAGS resident_flags; /* See above. */
757 /* 23 */		s8 reservedR;	    /* Reserved/alignment to 8-byte
758 					       boundary. */
759 /* 24 */		void *resident_end[0]; /* Use offsetof(ATTR_RECORD,
760 						  resident_end) to get size of
761 						  a resident attribute. */
762 		} __attribute__((__packed__));
763 		/* Non-resident attributes. */
764 		struct {
765 /* 16*/			leVCN lowest_vcn;	/* Lowest valid virtual cluster number
766 				for this portion of the attribute value or
767 				0 if this is the only extent (usually the
768 				case). - Only when an attribute list is used
769 				does lowest_vcn != 0 ever occur. */
770 /* 24*/			leVCN highest_vcn; /* Highest valid vcn of this extent of
771 				the attribute value. - Usually there is only one
772 				portion, so this usually equals the attribute
773 				value size in clusters minus 1. Can be -1 for
774 				zero length files. Can be 0 for "single extent"
775 				attributes. */
776 /* 32*/			le16 mapping_pairs_offset; /* Byte offset from the
777 				beginning of the structure to the mapping pairs
778 				array which contains the mappings between the
779 				vcns and the logical cluster numbers (lcns).
780 				When creating, place this at the end of this
781 				record header aligned to 8-byte boundary. */
782 /* 34*/			u8 compression_unit; /* The compression unit expressed
783 				as the log to the base 2 of the number of
784 				clusters in a compression unit. 0 means not
785 				compressed. (This effectively limits the
786 				compression unit size to be a power of two
787 				clusters.) WinNT4 only uses a value of 4. */
788 /* 35*/			u8 reserved1[5];	/* Align to 8-byte boundary. */
789 /* The sizes below are only used when lowest_vcn is zero, as otherwise it would
790    be difficult to keep them up-to-date.*/
791 /* 40*/			sle64 allocated_size;	/* Byte size of disk space
792 				allocated to hold the attribute value. Always
793 				is a multiple of the cluster size. When a file
794 				is compressed, this field is a multiple of the
795 				compression block size (2^compression_unit) and
796 				it represents the logically allocated space
797 				rather than the actual on disk usage. For this
798 				use the compressed_size (see below). */
799 /* 48*/			sle64 data_size;	/* Byte size of the attribute
800 				value. Can be larger than allocated_size if
801 				attribute value is compressed or sparse. */
802 /* 56*/			sle64 initialized_size;	/* Byte size of initialized
803 				portion of the attribute value. Usually equals
804 				data_size. */
805 /* 64 */		void *non_resident_end[0]; /* Use offsetof(ATTR_RECORD,
806 						      non_resident_end) to get
807 						      size of a non resident
808 						      attribute. */
809 /* sizeof(uncompressed attr) = 64*/
810 /* 64*/			sle64 compressed_size;	/* Byte size of the attribute
811 				value after compression. Only present when
812 				compressed. Always is a multiple of the
813 				cluster size. Represents the actual amount of
814 				disk space being used on the disk. */
815 /* 72 */		void *compressed_end[0];
816 				/* Use offsetof(ATTR_RECORD, compressed_end) to
817 				   get size of a compressed attribute. */
818 /* sizeof(compressed attr) = 72*/
819 		} __attribute__((__packed__));
820 	} __attribute__((__packed__));
821 } __attribute__((__packed__)) ATTR_RECORD;
822 
823 typedef ATTR_RECORD ATTR_REC;
824 
825 /**
826  * enum FILE_ATTR_FLAGS - File attribute flags (32-bit).
827  */
828 typedef enum {
829 	/*
830 	 * These flags are only present in the STANDARD_INFORMATION attribute
831 	 * (in the field file_attributes).
832 	 */
833 	FILE_ATTR_READONLY		= const_cpu_to_le32(0x00000001),
834 	FILE_ATTR_HIDDEN		= const_cpu_to_le32(0x00000002),
835 	FILE_ATTR_SYSTEM		= const_cpu_to_le32(0x00000004),
836 	/* Old DOS volid. Unused in NT.	= const_cpu_to_le32(0x00000008), */
837 
838 	FILE_ATTR_DIRECTORY		= const_cpu_to_le32(0x00000010),
839 	/* FILE_ATTR_DIRECTORY is not considered valid in NT. It is reserved
840 	   for the DOS SUBDIRECTORY flag. */
841 	FILE_ATTR_ARCHIVE		= const_cpu_to_le32(0x00000020),
842 	FILE_ATTR_DEVICE		= const_cpu_to_le32(0x00000040),
843 	FILE_ATTR_NORMAL		= const_cpu_to_le32(0x00000080),
844 
845 	FILE_ATTR_TEMPORARY		= const_cpu_to_le32(0x00000100),
846 	FILE_ATTR_SPARSE_FILE		= const_cpu_to_le32(0x00000200),
847 	FILE_ATTR_REPARSE_POINT		= const_cpu_to_le32(0x00000400),
848 	FILE_ATTR_COMPRESSED		= const_cpu_to_le32(0x00000800),
849 
850 	FILE_ATTR_OFFLINE		= const_cpu_to_le32(0x00001000),
851 	FILE_ATTR_NOT_CONTENT_INDEXED	= const_cpu_to_le32(0x00002000),
852 	FILE_ATTR_ENCRYPTED		= const_cpu_to_le32(0x00004000),
853 		/* Supposed to mean no data locally, possibly repurposed */
854 	FILE_ATTRIBUTE_RECALL_ON_OPEN	= const_cpu_to_le32(0x00040000),
855 
856 	FILE_ATTR_VALID_FLAGS		= const_cpu_to_le32(0x00047fb7),
857 	/* FILE_ATTR_VALID_FLAGS masks out the old DOS VolId and the
858 	   FILE_ATTR_DEVICE and preserves everything else. This mask
859 	   is used to obtain all flags that are valid for reading. */
860 	FILE_ATTR_VALID_SET_FLAGS	= const_cpu_to_le32(0x000031a7),
861 	/* FILE_ATTR_VALID_SET_FLAGS masks out the old DOS VolId, the
862 	   FILE_ATTR_DEVICE, FILE_ATTR_DIRECTORY, FILE_ATTR_SPARSE_FILE,
863 	   FILE_ATTR_REPARSE_POINT, FILE_ATRE_COMPRESSED and FILE_ATTR_ENCRYPTED
864 	   and preserves the rest. This mask is used to to obtain all flags that
865 	   are valid for setting. */
866 
867 	/**
868 	 * FILE_ATTR_I30_INDEX_PRESENT - Is it a directory?
869 	 *
870 	 * This is a copy of the MFT_RECORD_IS_DIRECTORY bit from the mft
871 	 * record, telling us whether this is a directory or not, i.e. whether
872 	 * it has an index root attribute named "$I30" or not.
873 	 *
874 	 * This flag is only present in the FILE_NAME attribute (in the
875 	 * file_attributes field).
876 	 */
877 	FILE_ATTR_I30_INDEX_PRESENT	= const_cpu_to_le32(0x10000000),
878 
879 	/**
880 	 * FILE_ATTR_VIEW_INDEX_PRESENT - Does have a non-directory index?
881 	 *
882 	 * This is a copy of the MFT_RECORD_IS_VIEW_INDEX bit from the mft
883 	 * record, telling us whether this file has a view index present (eg.
884 	 * object id index, quota index, one of the security indexes and the
885 	 * reparse points index).
886 	 *
887 	 * This flag is only present in the $STANDARD_INFORMATION and
888 	 * $FILE_NAME attributes.
889 	 */
890 	FILE_ATTR_VIEW_INDEX_PRESENT	= const_cpu_to_le32(0x20000000),
891 } __attribute__((__packed__)) FILE_ATTR_FLAGS;
892 
893 /*
894  * NOTE on times in NTFS: All times are in MS standard time format, i.e. they
895  * are the number of 100-nanosecond intervals since 1st January 1601, 00:00:00
896  * universal coordinated time (UTC). (In Linux time starts 1st January 1970,
897  * 00:00:00 UTC and is stored as the number of 1-second intervals since then.)
898  */
899 
900 /**
901  * struct STANDARD_INFORMATION - Attribute: Standard information (0x10).
902  *
903  * NOTE: Always resident.
904  * NOTE: Present in all base file records on a volume.
905  * NOTE: There is conflicting information about the meaning of each of the time
906  *	 fields but the meaning as defined below has been verified to be
907  *	 correct by practical experimentation on Windows NT4 SP6a and is hence
908  *	 assumed to be the one and only correct interpretation.
909  */
910 typedef struct {
911 /*Ofs*/
912 /*  0*/	sle64 creation_time;		/* Time file was created. Updated when
913 					   a filename is changed(?). */
914 /*  8*/	sle64 last_data_change_time;	/* Time the data attribute was last
915 					   modified. */
916 /* 16*/	sle64 last_mft_change_time;	/* Time this mft record was last
917 					   modified. */
918 /* 24*/	sle64 last_access_time;		/* Approximate time when the file was
919 					   last accessed (obviously this is not
920 					   updated on read-only volumes). In
921 					   Windows this is only updated when
922 					   accessed if some time delta has
923 					   passed since the last update. Also,
924 					   last access times updates can be
925 					   disabled altogether for speed. */
926 /* 32*/	FILE_ATTR_FLAGS file_attributes; /* Flags describing the file. */
927 /* 36*/	union {
928 		/* NTFS 1.2 (and previous, presumably) */
929 		struct {
930 		/* 36 */ u8 reserved12[12];	/* Reserved/alignment to 8-byte
931 						   boundary. */
932 		/* 48 */ void *v1_end[0];	/* Marker for offsetof(). */
933 		} __attribute__((__packed__));
934 /* sizeof() = 48 bytes */
935 		/* NTFS 3.0 */
936 		struct {
937 /*
938  * If a volume has been upgraded from a previous NTFS version, then these
939  * fields are present only if the file has been accessed since the upgrade.
940  * Recognize the difference by comparing the length of the resident attribute
941  * value. If it is 48, then the following fields are missing. If it is 72 then
942  * the fields are present. Maybe just check like this:
943  *	if (resident.ValueLength < sizeof(STANDARD_INFORMATION)) {
944  *		Assume NTFS 1.2- format.
945  *		If (volume version is 3.0+)
946  *			Upgrade attribute to NTFS 3.0 format.
947  *		else
948  *			Use NTFS 1.2- format for access.
949  *	} else
950  *		Use NTFS 3.0 format for access.
951  * Only problem is that it might be legal to set the length of the value to
952  * arbitrarily large values thus spoiling this check. - But chkdsk probably
953  * views that as a corruption, assuming that it behaves like this for all
954  * attributes.
955  */
956 		/* 36*/	le32 maximum_versions;	/* Maximum allowed versions for
957 				file. Zero if version numbering is disabled. */
958 		/* 40*/	le32 version_number;	/* This file's version (if any).
959 				Set to zero if maximum_versions is zero. */
960 		/* 44*/	le32 class_id;		/* Class id from bidirectional
961 				class id index (?). */
962 		/* 48*/	le32 owner_id;		/* Owner_id of the user owning
963 				the file. Translate via $Q index in FILE_Extend
964 				/$Quota to the quota control entry for the user
965 				owning the file. Zero if quotas are disabled. */
966 		/* 52*/	le32 security_id;	/* Security_id for the file.
967 				Translate via $SII index and $SDS data stream
968 				in FILE_Secure to the security descriptor. */
969 		/* 56*/	le64 quota_charged;	/* Byte size of the charge to
970 				the quota for all streams of the file. Note: Is
971 				zero if quotas are disabled. */
972 		/* 64*/	le64 usn;		/* Last update sequence number
973 				of the file. This is a direct index into the
974 				change (aka usn) journal file. It is zero if
975 				the usn journal is disabled.
976 				NOTE: To disable the journal need to delete
977 				the journal file itself and to then walk the
978 				whole mft and set all Usn entries in all mft
979 				records to zero! (This can take a while!)
980 				The journal is FILE_Extend/$UsnJrnl. Win2k
981 				will recreate the journal and initiate
982 				logging if necessary when mounting the
983 				partition. This, in contrast to disabling the
984 				journal is a very fast process, so the user
985 				won't even notice it. */
986 		/* 72*/ void *v3_end[0]; /* Marker for offsetof(). */
987 		} __attribute__((__packed__));
988 	} __attribute__((__packed__));
989 /* sizeof() = 72 bytes (NTFS 3.0) */
990 } __attribute__((__packed__)) STANDARD_INFORMATION;
991 
992 /**
993  * struct ATTR_LIST_ENTRY - Attribute: Attribute list (0x20).
994  *
995  * - Can be either resident or non-resident.
996  * - Value consists of a sequence of variable length, 8-byte aligned,
997  * ATTR_LIST_ENTRY records.
998  * - The attribute list attribute contains one entry for each attribute of
999  * the file in which the list is located, except for the list attribute
1000  * itself. The list is sorted: first by attribute type, second by attribute
1001  * name (if present), third by instance number. The extents of one
1002  * non-resident attribute (if present) immediately follow after the initial
1003  * extent. They are ordered by lowest_vcn and have their instance set to zero.
1004  * It is not allowed to have two attributes with all sorting keys equal.
1005  * - Further restrictions:
1006  *	- If not resident, the vcn to lcn mapping array has to fit inside the
1007  *	  base mft record.
1008  *	- The attribute list attribute value has a maximum size of 256kb. This
1009  *	  is imposed by the Windows cache manager.
1010  * - Attribute lists are only used when the attributes of mft record do not
1011  * fit inside the mft record despite all attributes (that can be made
1012  * non-resident) having been made non-resident. This can happen e.g. when:
1013  *	- File has a large number of hard links (lots of file name
1014  *	  attributes present).
1015  *	- The mapping pairs array of some non-resident attribute becomes so
1016  *	  large due to fragmentation that it overflows the mft record.
1017  *	- The security descriptor is very complex (not applicable to
1018  *	  NTFS 3.0 volumes).
1019  *	- There are many named streams.
1020  */
1021 typedef struct {
1022 /*Ofs*/
1023 /*  0*/	ATTR_TYPES type;	/* Type of referenced attribute. */
1024 /*  4*/	le16 length;		/* Byte size of this entry. */
1025 /*  6*/	u8 name_length;		/* Size in Unicode chars of the name of the
1026 				   attribute or 0 if unnamed. */
1027 /*  7*/	u8 name_offset;		/* Byte offset to beginning of attribute name
1028 				   (always set this to where the name would
1029 				   start even if unnamed). */
1030 /*  8*/	leVCN lowest_vcn;	/* Lowest virtual cluster number of this portion
1031 				   of the attribute value. This is usually 0. It
1032 				   is non-zero for the case where one attribute
1033 				   does not fit into one mft record and thus
1034 				   several mft records are allocated to hold
1035 				   this attribute. In the latter case, each mft
1036 				   record holds one extent of the attribute and
1037 				   there is one attribute list entry for each
1038 				   extent. NOTE: This is DEFINITELY a signed
1039 				   value! The windows driver uses cmp, followed
1040 				   by jg when comparing this, thus it treats it
1041 				   as signed. */
1042 /* 16*/	leMFT_REF mft_reference;/* The reference of the mft record holding
1043 				   the ATTR_RECORD for this portion of the
1044 				   attribute value. */
1045 /* 24*/	le16 instance;		/* If lowest_vcn = 0, the instance of the
1046 				   attribute being referenced; otherwise 0. */
1047 /* 26*/	ntfschar name[0];	/* Use when creating only. When reading use
1048 				   name_offset to determine the location of the
1049 				   name. */
1050 /* sizeof() = 26 + (attribute_name_length * 2) bytes */
1051 } __attribute__((__packed__)) ATTR_LIST_ENTRY;
1052 
1053 /*
1054  * The maximum allowed length for a file name.
1055  */
1056 #define NTFS_MAX_NAME_LEN	255
1057 
1058 /**
1059  * enum FILE_NAME_TYPE_FLAGS - Possible namespaces for filenames in ntfs.
1060  * (8-bit).
1061  */
1062 typedef enum {
1063 	FILE_NAME_POSIX			= 0x00,
1064 		/* This is the largest namespace. It is case sensitive and
1065 		   allows all Unicode characters except for: '\0' and '/'.
1066 		   Beware that in WinNT/2k files which eg have the same name
1067 		   except for their case will not be distinguished by the
1068 		   standard utilities and thus a "del filename" will delete
1069 		   both "filename" and "fileName" without warning. */
1070 	FILE_NAME_WIN32			= 0x01,
1071 		/* The standard WinNT/2k NTFS long filenames. Case insensitive.
1072 		   All Unicode chars except: '\0', '"', '*', '/', ':', '<',
1073 		   '>', '?', '\' and '|'.  Trailing dots and spaces are allowed,
1074 		   even though on Windows a filename with such a suffix can only
1075 		   be created and accessed using a WinNT-style path, i.e.
1076 		   \\?\-prefixed.  (If a regular path is used, Windows will
1077 		   strip the trailing dots and spaces, which makes such
1078 		   filenames incompatible with most Windows software.) */
1079 	FILE_NAME_DOS			= 0x02,
1080 		/* The standard DOS filenames (8.3 format). Uppercase only.
1081 		   All 8-bit characters greater space, except: '"', '*', '+',
1082 		   ',', '/', ':', ';', '<', '=', '>', '?' and '\'.  Trailing
1083 		   dots and spaces are forbidden. */
1084 	FILE_NAME_WIN32_AND_DOS		= 0x03,
1085 		/* 3 means that both the Win32 and the DOS filenames are
1086 		   identical and hence have been saved in this single filename
1087 		   record. */
1088 } __attribute__((__packed__)) FILE_NAME_TYPE_FLAGS;
1089 
1090 /**
1091  * struct FILE_NAME_ATTR - Attribute: Filename (0x30).
1092  *
1093  * NOTE: Always resident.
1094  * NOTE: All fields, except the parent_directory, are only updated when the
1095  *	 filename is changed. Until then, they just become out of sync with
1096  *	 reality and the more up to date values are present in the standard
1097  *	 information attribute.
1098  * NOTE: There is conflicting information about the meaning of each of the time
1099  *	 fields but the meaning as defined below has been verified to be
1100  *	 correct by practical experimentation on Windows NT4 SP6a and is hence
1101  *	 assumed to be the one and only correct interpretation.
1102  */
1103 typedef struct {
1104 /*hex ofs*/
1105 /*  0*/	leMFT_REF parent_directory;	/* Directory this filename is
1106 					   referenced from. */
1107 /*  8*/	sle64 creation_time;		/* Time file was created. */
1108 /* 10*/	sle64 last_data_change_time;	/* Time the data attribute was last
1109 					   modified. */
1110 /* 18*/	sle64 last_mft_change_time;	/* Time this mft record was last
1111 					   modified. */
1112 /* 20*/	sle64 last_access_time;		/* Last time this mft record was
1113 					   accessed. */
1114 /* 28*/	sle64 allocated_size;		/* Byte size of on-disk allocated space
1115 					   for the data attribute.  So for
1116 					   normal $DATA, this is the
1117 					   allocated_size from the unnamed
1118 					   $DATA attribute and for compressed
1119 					   and/or sparse $DATA, this is the
1120 					   compressed_size from the unnamed
1121 					   $DATA attribute.  NOTE: This is a
1122 					   multiple of the cluster size. */
1123 /* 30*/	sle64 data_size;			/* Byte size of actual data in data
1124 					   attribute. */
1125 /* 38*/	FILE_ATTR_FLAGS file_attributes;	/* Flags describing the file. */
1126 /* 3c*/	union {
1127 	/* 3c*/	struct {
1128 		/* 3c*/	le16 packed_ea_size;	/* Size of the buffer needed to
1129 						   pack the extended attributes
1130 						   (EAs), if such are present.*/
1131 		/* 3e*/	le16 reserved;		/* Reserved for alignment. */
1132 		} __attribute__((__packed__));
1133 	/* 3c*/	le32 reparse_point_tag;		/* Type of reparse point,
1134 						   present only in reparse
1135 						   points and only if there are
1136 						   no EAs. */
1137 	} __attribute__((__packed__));
1138 /* 40*/	u8 file_name_length;			/* Length of file name in
1139 						   (Unicode) characters. */
1140 /* 41*/	FILE_NAME_TYPE_FLAGS file_name_type;	/* Namespace of the file name.*/
1141 /* 42*/	ntfschar file_name[0];			/* File name in Unicode. */
1142 } __attribute__((__packed__)) FILE_NAME_ATTR;
1143 
1144 /**
1145  * struct GUID - GUID structures store globally unique identifiers (GUID).
1146  *
1147  * A GUID is a 128-bit value consisting of one group of eight hexadecimal
1148  * digits, followed by three groups of four hexadecimal digits each, followed
1149  * by one group of twelve hexadecimal digits. GUIDs are Microsoft's
1150  * implementation of the distributed computing environment (DCE) universally
1151  * unique identifier (UUID).
1152  *
1153  * Example of a GUID:
1154  *	1F010768-5A73-BC91-0010-A52216A7227B
1155  */
1156 typedef struct {
1157 	le32 data1;	/* The first eight hexadecimal digits of the GUID. */
1158 	le16 data2;	/* The first group of four hexadecimal digits. */
1159 	le16 data3;	/* The second group of four hexadecimal digits. */
1160 	u8 data4[8];	/* The first two bytes are the third group of four
1161 			   hexadecimal digits. The remaining six bytes are the
1162 			   final 12 hexadecimal digits. */
1163 } __attribute__((__packed__)) GUID;
1164 
1165 /**
1166  * struct OBJ_ID_INDEX_DATA - FILE_Extend/$ObjId contains an index named $O.
1167  *
1168  * This index contains all object_ids present on the volume as the index keys
1169  * and the corresponding mft_record numbers as the index entry data parts.
1170  *
1171  * The data part (defined below) also contains three other object_ids:
1172  *	birth_volume_id - object_id of FILE_Volume on which the file was first
1173  *			  created. Optional (i.e. can be zero).
1174  *	birth_object_id - object_id of file when it was first created. Usually
1175  *			  equals the object_id. Optional (i.e. can be zero).
1176  *	domain_id	- Reserved (always zero).
1177  */
1178 typedef struct {
1179 	leMFT_REF mft_reference;	/* Mft record containing the object_id
1180 					   in the index entry key. */
1181 	union {
1182 		struct {
1183 			GUID birth_volume_id;
1184 			GUID birth_object_id;
1185 			GUID domain_id;
1186 		} __attribute__((__packed__));
1187 		u8 extended_info[48];
1188 	} __attribute__((__packed__));
1189 } __attribute__((__packed__)) OBJ_ID_INDEX_DATA;
1190 
1191 /**
1192  * struct OBJECT_ID_ATTR - Attribute: Object id (NTFS 3.0+) (0x40).
1193  *
1194  * NOTE: Always resident.
1195  */
1196 typedef struct {
1197 	GUID object_id;				/* Unique id assigned to the
1198 						   file.*/
1199 	/* The following fields are optional. The attribute value size is 16
1200 	   bytes, i.e. sizeof(GUID), if these are not present at all. Note,
1201 	   the entries can be present but one or more (or all) can be zero
1202 	   meaning that that particular value(s) is(are) not defined. Note,
1203 	   when the fields are missing here, it is well possible that they are
1204 	   to be found within the $Extend/$ObjId system file indexed under the
1205 	   above object_id. */
1206 	union {
1207 		struct {
1208 			GUID birth_volume_id;	/* Unique id of volume on which
1209 						   the file was first created.*/
1210 			GUID birth_object_id;	/* Unique id of file when it was
1211 						   first created. */
1212 			GUID domain_id;		/* Reserved, zero. */
1213 		} __attribute__((__packed__));
1214 		u8 extended_info[48];
1215 	} __attribute__((__packed__));
1216 } __attribute__((__packed__)) OBJECT_ID_ATTR;
1217 
1218 #if 0
1219 /**
1220  * enum IDENTIFIER_AUTHORITIES -
1221  *
1222  * The pre-defined IDENTIFIER_AUTHORITIES used as SID_IDENTIFIER_AUTHORITY in
1223  * the SID structure (see below).
1224  */
1225 typedef enum {					/* SID string prefix. */
1226 	SECURITY_NULL_SID_AUTHORITY	= {0, 0, 0, 0, 0, 0},	/* S-1-0 */
1227 	SECURITY_WORLD_SID_AUTHORITY	= {0, 0, 0, 0, 0, 1},	/* S-1-1 */
1228 	SECURITY_LOCAL_SID_AUTHORITY	= {0, 0, 0, 0, 0, 2},	/* S-1-2 */
1229 	SECURITY_CREATOR_SID_AUTHORITY	= {0, 0, 0, 0, 0, 3},	/* S-1-3 */
1230 	SECURITY_NON_UNIQUE_AUTHORITY	= {0, 0, 0, 0, 0, 4},	/* S-1-4 */
1231 	SECURITY_NT_SID_AUTHORITY	= {0, 0, 0, 0, 0, 5},	/* S-1-5 */
1232 } IDENTIFIER_AUTHORITIES;
1233 #endif
1234 
1235 /**
1236  * enum RELATIVE_IDENTIFIERS -
1237  *
1238  * These relative identifiers (RIDs) are used with the above identifier
1239  * authorities to make up universal well-known SIDs.
1240  *
1241  * Note: The relative identifier (RID) refers to the portion of a SID, which
1242  * identifies a user or group in relation to the authority that issued the SID.
1243  * For example, the universal well-known SID Creator Owner ID (S-1-3-0) is
1244  * made up of the identifier authority SECURITY_CREATOR_SID_AUTHORITY (3) and
1245  * the relative identifier SECURITY_CREATOR_OWNER_RID (0).
1246  */
1247 typedef enum {					/* Identifier authority. */
1248 	SECURITY_NULL_RID		  = 0,	/* S-1-0 */
1249 	SECURITY_WORLD_RID		  = 0,	/* S-1-1 */
1250 	SECURITY_LOCAL_RID		  = 0,	/* S-1-2 */
1251 
1252 	SECURITY_CREATOR_OWNER_RID	  = 0,	/* S-1-3 */
1253 	SECURITY_CREATOR_GROUP_RID	  = 1,	/* S-1-3 */
1254 
1255 	SECURITY_CREATOR_OWNER_SERVER_RID = 2,	/* S-1-3 */
1256 	SECURITY_CREATOR_GROUP_SERVER_RID = 3,	/* S-1-3 */
1257 
1258 	SECURITY_DIALUP_RID		  = 1,
1259 	SECURITY_NETWORK_RID		  = 2,
1260 	SECURITY_BATCH_RID		  = 3,
1261 	SECURITY_INTERACTIVE_RID	  = 4,
1262 	SECURITY_SERVICE_RID		  = 6,
1263 	SECURITY_ANONYMOUS_LOGON_RID	  = 7,
1264 	SECURITY_PROXY_RID		  = 8,
1265 	SECURITY_ENTERPRISE_CONTROLLERS_RID=9,
1266 	SECURITY_SERVER_LOGON_RID	  = 9,
1267 	SECURITY_PRINCIPAL_SELF_RID	  = 0xa,
1268 	SECURITY_AUTHENTICATED_USER_RID	  = 0xb,
1269 	SECURITY_RESTRICTED_CODE_RID	  = 0xc,
1270 	SECURITY_TERMINAL_SERVER_RID	  = 0xd,
1271 
1272 	SECURITY_LOGON_IDS_RID		  = 5,
1273 	SECURITY_LOGON_IDS_RID_COUNT	  = 3,
1274 
1275 	SECURITY_LOCAL_SYSTEM_RID	  = 0x12,
1276 
1277 	SECURITY_NT_NON_UNIQUE		  = 0x15,
1278 
1279 	SECURITY_BUILTIN_DOMAIN_RID	  = 0x20,
1280 
1281 	/*
1282 	 * Well-known domain relative sub-authority values (RIDs).
1283 	 */
1284 
1285 	/* Users. */
1286 	DOMAIN_USER_RID_ADMIN		  = 0x1f4,
1287 	DOMAIN_USER_RID_GUEST		  = 0x1f5,
1288 	DOMAIN_USER_RID_KRBTGT		  = 0x1f6,
1289 
1290 	/* Groups. */
1291 	DOMAIN_GROUP_RID_ADMINS		  = 0x200,
1292 	DOMAIN_GROUP_RID_USERS		  = 0x201,
1293 	DOMAIN_GROUP_RID_GUESTS		  = 0x202,
1294 	DOMAIN_GROUP_RID_COMPUTERS	  = 0x203,
1295 	DOMAIN_GROUP_RID_CONTROLLERS	  = 0x204,
1296 	DOMAIN_GROUP_RID_CERT_ADMINS	  = 0x205,
1297 	DOMAIN_GROUP_RID_SCHEMA_ADMINS	  = 0x206,
1298 	DOMAIN_GROUP_RID_ENTERPRISE_ADMINS= 0x207,
1299 	DOMAIN_GROUP_RID_POLICY_ADMINS	  = 0x208,
1300 
1301 	/* Aliases. */
1302 	DOMAIN_ALIAS_RID_ADMINS		  = 0x220,
1303 	DOMAIN_ALIAS_RID_USERS		  = 0x221,
1304 	DOMAIN_ALIAS_RID_GUESTS		  = 0x222,
1305 	DOMAIN_ALIAS_RID_POWER_USERS	  = 0x223,
1306 
1307 	DOMAIN_ALIAS_RID_ACCOUNT_OPS	  = 0x224,
1308 	DOMAIN_ALIAS_RID_SYSTEM_OPS	  = 0x225,
1309 	DOMAIN_ALIAS_RID_PRINT_OPS	  = 0x226,
1310 	DOMAIN_ALIAS_RID_BACKUP_OPS	  = 0x227,
1311 
1312 	DOMAIN_ALIAS_RID_REPLICATOR	  = 0x228,
1313 	DOMAIN_ALIAS_RID_RAS_SERVERS	  = 0x229,
1314 	DOMAIN_ALIAS_RID_PREW2KCOMPACCESS = 0x22a,
1315 } RELATIVE_IDENTIFIERS;
1316 
1317 /*
1318  * The universal well-known SIDs:
1319  *
1320  *	NULL_SID			S-1-0-0
1321  *	WORLD_SID			S-1-1-0
1322  *	LOCAL_SID			S-1-2-0
1323  *	CREATOR_OWNER_SID		S-1-3-0
1324  *	CREATOR_GROUP_SID		S-1-3-1
1325  *	CREATOR_OWNER_SERVER_SID	S-1-3-2
1326  *	CREATOR_GROUP_SERVER_SID	S-1-3-3
1327  *
1328  *	(Non-unique IDs)		S-1-4
1329  *
1330  * NT well-known SIDs:
1331  *
1332  *	NT_AUTHORITY_SID	S-1-5
1333  *	DIALUP_SID		S-1-5-1
1334  *
1335  *	NETWORD_SID		S-1-5-2
1336  *	BATCH_SID		S-1-5-3
1337  *	INTERACTIVE_SID		S-1-5-4
1338  *	SERVICE_SID		S-1-5-6
1339  *	ANONYMOUS_LOGON_SID	S-1-5-7		(aka null logon session)
1340  *	PROXY_SID		S-1-5-8
1341  *	SERVER_LOGON_SID	S-1-5-9		(aka domain controller account)
1342  *	SELF_SID		S-1-5-10	(self RID)
1343  *	AUTHENTICATED_USER_SID	S-1-5-11
1344  *	RESTRICTED_CODE_SID	S-1-5-12	(running restricted code)
1345  *	TERMINAL_SERVER_SID	S-1-5-13	(running on terminal server)
1346  *
1347  *	(Logon IDs)		S-1-5-5-X-Y
1348  *
1349  *	(NT non-unique IDs)	S-1-5-0x15-...
1350  *
1351  *	(Built-in domain)	S-1-5-0x20
1352  */
1353 
1354 /**
1355  * union SID_IDENTIFIER_AUTHORITY - A 48-bit value used in the SID structure
1356  *
1357  * NOTE: This is stored as a big endian number.
1358  */
1359 typedef union {
1360 	struct {
1361 		be16 high_part;		/* High 16-bits. */
1362 		be32 low_part;		/* Low 32-bits. */
1363 	} __attribute__((__packed__));
1364 	u8 value[6];			/* Value as individual bytes. */
1365 } __attribute__((__packed__)) SID_IDENTIFIER_AUTHORITY;
1366 
1367 /**
1368  * struct SID -
1369  *
1370  * The SID structure is a variable-length structure used to uniquely identify
1371  * users or groups. SID stands for security identifier.
1372  *
1373  * The standard textual representation of the SID is of the form:
1374  *	S-R-I-S-S...
1375  * Where:
1376  *    - The first "S" is the literal character 'S' identifying the following
1377  *	digits as a SID.
1378  *    - R is the revision level of the SID expressed as a sequence of digits
1379  *	in decimal.
1380  *    - I is the 48-bit identifier_authority, expressed as digits in decimal,
1381  *	if I < 2^32, or hexadecimal prefixed by "0x", if I >= 2^32.
1382  *    - S... is one or more sub_authority values, expressed as digits in
1383  *	decimal.
1384  *
1385  * Example SID; the domain-relative SID of the local Administrators group on
1386  * Windows NT/2k:
1387  *	S-1-5-32-544
1388  * This translates to a SID with:
1389  *	revision = 1,
1390  *	sub_authority_count = 2,
1391  *	identifier_authority = {0,0,0,0,0,5},	// SECURITY_NT_AUTHORITY
1392  *	sub_authority[0] = 32,			// SECURITY_BUILTIN_DOMAIN_RID
1393  *	sub_authority[1] = 544			// DOMAIN_ALIAS_RID_ADMINS
1394  */
1395 typedef struct {
1396 	u8 revision;
1397 	u8 sub_authority_count;
1398 	SID_IDENTIFIER_AUTHORITY identifier_authority;
1399 	le32 sub_authority[1];		/* At least one sub_authority. */
1400 } __attribute__((__packed__)) SID;
1401 
1402 /**
1403  * enum SID_CONSTANTS - Current constants for SIDs.
1404  */
1405 typedef enum {
1406 	SID_REVISION			=  1,	/* Current revision level. */
1407 	SID_MAX_SUB_AUTHORITIES		= 15,	/* Maximum number of those. */
1408 	SID_RECOMMENDED_SUB_AUTHORITIES	=  1,	/* Will change to around 6 in
1409 						   a future revision. */
1410 } SID_CONSTANTS;
1411 
1412 /**
1413  * enum ACE_TYPES - The predefined ACE types (8-bit, see below).
1414  */
1415 typedef enum {
1416 	ACCESS_MIN_MS_ACE_TYPE		= 0,
1417 	ACCESS_ALLOWED_ACE_TYPE		= 0,
1418 	ACCESS_DENIED_ACE_TYPE		= 1,
1419 	SYSTEM_AUDIT_ACE_TYPE		= 2,
1420 	SYSTEM_ALARM_ACE_TYPE		= 3, /* Not implemented as of Win2k. */
1421 	ACCESS_MAX_MS_V2_ACE_TYPE	= 3,
1422 
1423 	ACCESS_ALLOWED_COMPOUND_ACE_TYPE= 4,
1424 	ACCESS_MAX_MS_V3_ACE_TYPE	= 4,
1425 
1426 	/* The following are Win2k only. */
1427 	ACCESS_MIN_MS_OBJECT_ACE_TYPE	= 5,
1428 	ACCESS_ALLOWED_OBJECT_ACE_TYPE	= 5,
1429 	ACCESS_DENIED_OBJECT_ACE_TYPE	= 6,
1430 	SYSTEM_AUDIT_OBJECT_ACE_TYPE	= 7,
1431 	SYSTEM_ALARM_OBJECT_ACE_TYPE	= 8,
1432 	ACCESS_MAX_MS_OBJECT_ACE_TYPE	= 8,
1433 
1434 	ACCESS_MAX_MS_V4_ACE_TYPE	= 8,
1435 
1436 	/* This one is for WinNT&2k. */
1437 	ACCESS_MAX_MS_ACE_TYPE		= 8,
1438 
1439 	/* Windows XP and later */
1440 	ACCESS_ALLOWED_CALLBACK_ACE_TYPE	= 9,
1441 	ACCESS_DENIED_CALLBACK_ACE_TYPE		= 10,
1442 	ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE	= 11,
1443 	ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE	= 12,
1444 	SYSTEM_AUDIT_CALLBACK_ACE_TYPE		= 13,
1445 	SYSTEM_ALARM_CALLBACK_ACE_TYPE		= 14, /* reserved */
1446 	SYSTEM_AUDIT_CALLBACK_OBJECT_ACE_TYPE   = 15,
1447 	SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE   = 16, /* reserved */
1448 
1449 	/* Windows Vista and later */
1450 	SYSTEM_MANDATORY_LABEL_ACE_TYPE		= 17,
1451 
1452 	/* Windows 8 and later */
1453 	SYSTEM_RESOURCE_ATTRIBUTE_ACE_TYPE	= 18,
1454 	SYSTEM_SCOPED_POLICY_ID_ACE_TYPE	= 19,
1455 
1456 	/* Windows 10 and later */
1457 	SYSTEM_PROCESS_TRUST_LABEL_ACE_TYPE	= 20,
1458 } __attribute__((__packed__)) ACE_TYPES;
1459 
1460 /**
1461  * enum ACE_FLAGS - The ACE flags (8-bit) for audit and inheritance.
1462  *
1463  * SUCCESSFUL_ACCESS_ACE_FLAG is only used with system audit and alarm ACE
1464  * types to indicate that a message is generated (in Windows!) for successful
1465  * accesses.
1466  *
1467  * FAILED_ACCESS_ACE_FLAG is only used with system audit and alarm ACE types
1468  * to indicate that a message is generated (in Windows!) for failed accesses.
1469  */
1470 typedef enum {
1471 	/* The inheritance flags. */
1472 	OBJECT_INHERIT_ACE		= 0x01,
1473 	CONTAINER_INHERIT_ACE		= 0x02,
1474 	NO_PROPAGATE_INHERIT_ACE	= 0x04,
1475 	INHERIT_ONLY_ACE		= 0x08,
1476 	INHERITED_ACE			= 0x10,	/* Win2k only. */
1477 	VALID_INHERIT_FLAGS		= 0x1f,
1478 
1479 	/* The audit flags. */
1480 	SUCCESSFUL_ACCESS_ACE_FLAG	= 0x40,
1481 	FAILED_ACCESS_ACE_FLAG		= 0x80,
1482 } __attribute__((__packed__)) ACE_FLAGS;
1483 
1484 /**
1485  * struct ACE_HEADER -
1486  *
1487  * An ACE is an access-control entry in an access-control list (ACL).
1488  * An ACE defines access to an object for a specific user or group or defines
1489  * the types of access that generate system-administration messages or alarms
1490  * for a specific user or group. The user or group is identified by a security
1491  * identifier (SID).
1492  *
1493  * Each ACE starts with an ACE_HEADER structure (aligned on 4-byte boundary),
1494  * which specifies the type and size of the ACE. The format of the subsequent
1495  * data depends on the ACE type.
1496  */
1497 typedef struct {
1498 	ACE_TYPES type;		/* Type of the ACE. */
1499 	ACE_FLAGS flags;	/* Flags describing the ACE. */
1500 	le16 size;		/* Size in bytes of the ACE. */
1501 } __attribute__((__packed__)) ACE_HEADER;
1502 
1503 /**
1504  * enum ACCESS_MASK - The access mask (32-bit).
1505  *
1506  * Defines the access rights.
1507  */
1508 typedef enum {
1509 	/*
1510 	 * The specific rights (bits 0 to 15). Depend on the type of the
1511 	 * object being secured by the ACE.
1512 	 */
1513 
1514 	/* Specific rights for files and directories are as follows: */
1515 
1516 	/* Right to read data from the file. (FILE) */
1517 	FILE_READ_DATA			= const_cpu_to_le32(0x00000001),
1518 	/* Right to list contents of a directory. (DIRECTORY) */
1519 	FILE_LIST_DIRECTORY		= const_cpu_to_le32(0x00000001),
1520 
1521 	/* Right to write data to the file. (FILE) */
1522 	FILE_WRITE_DATA			= const_cpu_to_le32(0x00000002),
1523 	/* Right to create a file in the directory. (DIRECTORY) */
1524 	FILE_ADD_FILE			= const_cpu_to_le32(0x00000002),
1525 
1526 	/* Right to append data to the file. (FILE) */
1527 	FILE_APPEND_DATA		= const_cpu_to_le32(0x00000004),
1528 	/* Right to create a subdirectory. (DIRECTORY) */
1529 	FILE_ADD_SUBDIRECTORY		= const_cpu_to_le32(0x00000004),
1530 
1531 	/* Right to read extended attributes. (FILE/DIRECTORY) */
1532 	FILE_READ_EA			= const_cpu_to_le32(0x00000008),
1533 
1534 	/* Right to write extended attributes. (FILE/DIRECTORY) */
1535 	FILE_WRITE_EA			= const_cpu_to_le32(0x00000010),
1536 
1537 	/* Right to execute a file. (FILE) */
1538 	FILE_EXECUTE			= const_cpu_to_le32(0x00000020),
1539 	/* Right to traverse the directory. (DIRECTORY) */
1540 	FILE_TRAVERSE			= const_cpu_to_le32(0x00000020),
1541 
1542 	/*
1543 	 * Right to delete a directory and all the files it contains (its
1544 	 * children), even if the files are read-only. (DIRECTORY)
1545 	 */
1546 	FILE_DELETE_CHILD		= const_cpu_to_le32(0x00000040),
1547 
1548 	/* Right to read file attributes. (FILE/DIRECTORY) */
1549 	FILE_READ_ATTRIBUTES		= const_cpu_to_le32(0x00000080),
1550 
1551 	/* Right to change file attributes. (FILE/DIRECTORY) */
1552 	FILE_WRITE_ATTRIBUTES		= const_cpu_to_le32(0x00000100),
1553 
1554 	/*
1555 	 * The standard rights (bits 16 to 23). Are independent of the type of
1556 	 * object being secured.
1557 	 */
1558 
1559 	/* Right to delete the object. */
1560 	DELETE				= const_cpu_to_le32(0x00010000),
1561 
1562 	/*
1563 	 * Right to read the information in the object's security descriptor,
1564 	 * not including the information in the SACL. I.e. right to read the
1565 	 * security descriptor and owner.
1566 	 */
1567 	READ_CONTROL			= const_cpu_to_le32(0x00020000),
1568 
1569 	/* Right to modify the DACL in the object's security descriptor. */
1570 	WRITE_DAC			= const_cpu_to_le32(0x00040000),
1571 
1572 	/* Right to change the owner in the object's security descriptor. */
1573 	WRITE_OWNER			= const_cpu_to_le32(0x00080000),
1574 
1575 	/*
1576 	 * Right to use the object for synchronization. Enables a process to
1577 	 * wait until the object is in the signalled state. Some object types
1578 	 * do not support this access right.
1579 	 */
1580 	SYNCHRONIZE			= const_cpu_to_le32(0x00100000),
1581 
1582 	/*
1583 	 * The following STANDARD_RIGHTS_* are combinations of the above for
1584 	 * convenience and are defined by the Win32 API.
1585 	 */
1586 
1587 	/* These are currently defined to READ_CONTROL. */
1588 	STANDARD_RIGHTS_READ		= const_cpu_to_le32(0x00020000),
1589 	STANDARD_RIGHTS_WRITE		= const_cpu_to_le32(0x00020000),
1590 	STANDARD_RIGHTS_EXECUTE		= const_cpu_to_le32(0x00020000),
1591 
1592 	/* Combines DELETE, READ_CONTROL, WRITE_DAC, and WRITE_OWNER access. */
1593 	STANDARD_RIGHTS_REQUIRED	= const_cpu_to_le32(0x000f0000),
1594 
1595 	/*
1596 	 * Combines DELETE, READ_CONTROL, WRITE_DAC, WRITE_OWNER, and
1597 	 * SYNCHRONIZE access.
1598 	 */
1599 	STANDARD_RIGHTS_ALL		= const_cpu_to_le32(0x001f0000),
1600 
1601 	/*
1602 	 * The access system ACL and maximum allowed access types (bits 24 to
1603 	 * 25, bits 26 to 27 are reserved).
1604 	 */
1605 	ACCESS_SYSTEM_SECURITY		= const_cpu_to_le32(0x01000000),
1606 	MAXIMUM_ALLOWED			= const_cpu_to_le32(0x02000000),
1607 
1608 	/*
1609 	 * The generic rights (bits 28 to 31). These map onto the standard and
1610 	 * specific rights.
1611 	 */
1612 
1613 	/* Read, write, and execute access. */
1614 	GENERIC_ALL			= const_cpu_to_le32(0x10000000),
1615 
1616 	/* Execute access. */
1617 	GENERIC_EXECUTE			= const_cpu_to_le32(0x20000000),
1618 
1619 	/*
1620 	 * Write access. For files, this maps onto:
1621 	 *	FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA |
1622 	 *	FILE_WRITE_EA | STANDARD_RIGHTS_WRITE | SYNCHRONIZE
1623 	 * For directories, the mapping has the same numerical value. See
1624 	 * above for the descriptions of the rights granted.
1625 	 */
1626 	GENERIC_WRITE			= const_cpu_to_le32(0x40000000),
1627 
1628 	/*
1629 	 * Read access. For files, this maps onto:
1630 	 *	FILE_READ_ATTRIBUTES | FILE_READ_DATA | FILE_READ_EA |
1631 	 *	STANDARD_RIGHTS_READ | SYNCHRONIZE
1632 	 * For directories, the mapping has the same numerical value. See
1633 	 * above for the descriptions of the rights granted.
1634 	 */
1635 	GENERIC_READ			= const_cpu_to_le32(0x80000000),
1636 } ACCESS_MASK;
1637 
1638 /**
1639  * struct GENERIC_MAPPING -
1640  *
1641  * The generic mapping array. Used to denote the mapping of each generic
1642  * access right to a specific access mask.
1643  *
1644  * FIXME: What exactly is this and what is it for? (AIA)
1645  */
1646 typedef struct {
1647 	ACCESS_MASK generic_read;
1648 	ACCESS_MASK generic_write;
1649 	ACCESS_MASK generic_execute;
1650 	ACCESS_MASK generic_all;
1651 } __attribute__((__packed__)) GENERIC_MAPPING;
1652 
1653 /*
1654  * The predefined ACE type structures are as defined below.
1655  */
1656 
1657 /**
1658  * struct ACCESS_DENIED_ACE -
1659  *
1660  * ACCESS_ALLOWED_ACE, ACCESS_DENIED_ACE, SYSTEM_AUDIT_ACE, SYSTEM_ALARM_ACE
1661  */
1662 typedef struct {
1663 /*  0	ACE_HEADER; -- Unfolded here as gcc doesn't like unnamed structs. */
1664 	ACE_TYPES type;		/* Type of the ACE. */
1665 	ACE_FLAGS flags;	/* Flags describing the ACE. */
1666 	le16 size;		/* Size in bytes of the ACE. */
1667 
1668 /*  4*/	ACCESS_MASK mask;	/* Access mask associated with the ACE. */
1669 /*  8*/	SID sid;		/* The SID associated with the ACE. */
1670 } __attribute__((__packed__)) ACCESS_ALLOWED_ACE, ACCESS_DENIED_ACE,
1671 			       SYSTEM_AUDIT_ACE, SYSTEM_ALARM_ACE;
1672 
1673 /**
1674  * enum OBJECT_ACE_FLAGS - The object ACE flags (32-bit).
1675  */
1676 typedef enum {
1677 	ACE_OBJECT_TYPE_PRESENT			= const_cpu_to_le32(1),
1678 	ACE_INHERITED_OBJECT_TYPE_PRESENT	= const_cpu_to_le32(2),
1679 } OBJECT_ACE_FLAGS;
1680 
1681 /**
1682  * struct ACCESS_ALLOWED_OBJECT_ACE -
1683  */
1684 typedef struct {
1685 /*  0	ACE_HEADER; -- Unfolded here as gcc doesn't like unnamed structs. */
1686 	ACE_TYPES type;		/* Type of the ACE. */
1687 	ACE_FLAGS flags;	/* Flags describing the ACE. */
1688 	le16 size;		/* Size in bytes of the ACE. */
1689 
1690 /*  4*/	ACCESS_MASK mask;	/* Access mask associated with the ACE. */
1691 /*  8*/	OBJECT_ACE_FLAGS object_flags;	/* Flags describing the object ACE. */
1692 /* 12*/	GUID object_type;
1693 /* 28*/	GUID inherited_object_type;
1694 /* 44*/	SID sid;		/* The SID associated with the ACE. */
1695 } __attribute__((__packed__)) ACCESS_ALLOWED_OBJECT_ACE,
1696 			       ACCESS_DENIED_OBJECT_ACE,
1697 			       SYSTEM_AUDIT_OBJECT_ACE,
1698 			       SYSTEM_ALARM_OBJECT_ACE;
1699 
1700 /**
1701  * struct ACL - An ACL is an access-control list (ACL).
1702  *
1703  * An ACL starts with an ACL header structure, which specifies the size of
1704  * the ACL and the number of ACEs it contains. The ACL header is followed by
1705  * zero or more access control entries (ACEs). The ACL as well as each ACE
1706  * are aligned on 4-byte boundaries.
1707  */
1708 typedef struct {
1709 	u8 revision;	/* Revision of this ACL. */
1710 	u8 alignment1;
1711 	le16 size;	/* Allocated space in bytes for ACL. Includes this
1712 			   header, the ACEs and the remaining free space. */
1713 	le16 ace_count;	/* Number of ACEs in the ACL. */
1714 	le16 alignment2;
1715 /* sizeof() = 8 bytes */
1716 } __attribute__((__packed__)) ACL;
1717 
1718 /**
1719  * enum ACL_CONSTANTS - Current constants for ACLs.
1720  */
1721 typedef enum {
1722 	/* Current revision. */
1723 	ACL_REVISION		= 2,
1724 	ACL_REVISION_DS		= 4,
1725 
1726 	/* History of revisions. */
1727 	ACL_REVISION1		= 1,
1728 	MIN_ACL_REVISION	= 2,
1729 	ACL_REVISION2		= 2,
1730 	ACL_REVISION3		= 3,
1731 	ACL_REVISION4		= 4,
1732 	MAX_ACL_REVISION	= 4,
1733 } ACL_CONSTANTS;
1734 
1735 /**
1736  * enum SECURITY_DESCRIPTOR_CONTROL -
1737  *
1738  * The security descriptor control flags (16-bit).
1739  *
1740  * SE_OWNER_DEFAULTED - This boolean flag, when set, indicates that the
1741  *	SID pointed to by the Owner field was provided by a
1742  *	defaulting mechanism rather than explicitly provided by the
1743  *	original provider of the security descriptor.  This may
1744  *	affect the treatment of the SID with respect to inheritance
1745  *	of an owner.
1746  *
1747  * SE_GROUP_DEFAULTED - This boolean flag, when set, indicates that the
1748  *	SID in the Group field was provided by a defaulting mechanism
1749  *	rather than explicitly provided by the original provider of
1750  *	the security descriptor.  This may affect the treatment of
1751  *	the SID with respect to inheritance of a primary group.
1752  *
1753  * SE_DACL_PRESENT - This boolean flag, when set, indicates that the
1754  *	security descriptor contains a discretionary ACL.  If this
1755  *	flag is set and the Dacl field of the SECURITY_DESCRIPTOR is
1756  *	null, then a null ACL is explicitly being specified.
1757  *
1758  * SE_DACL_DEFAULTED - This boolean flag, when set, indicates that the
1759  *	ACL pointed to by the Dacl field was provided by a defaulting
1760  *	mechanism rather than explicitly provided by the original
1761  *	provider of the security descriptor.  This may affect the
1762  *	treatment of the ACL with respect to inheritance of an ACL.
1763  *	This flag is ignored if the DaclPresent flag is not set.
1764  *
1765  * SE_SACL_PRESENT - This boolean flag, when set,  indicates that the
1766  *	security descriptor contains a system ACL pointed to by the
1767  *	Sacl field.  If this flag is set and the Sacl field of the
1768  *	SECURITY_DESCRIPTOR is null, then an empty (but present)
1769  *	ACL is being specified.
1770  *
1771  * SE_SACL_DEFAULTED - This boolean flag, when set, indicates that the
1772  *	ACL pointed to by the Sacl field was provided by a defaulting
1773  *	mechanism rather than explicitly provided by the original
1774  *	provider of the security descriptor.  This may affect the
1775  *	treatment of the ACL with respect to inheritance of an ACL.
1776  *	This flag is ignored if the SaclPresent flag is not set.
1777  *
1778  * SE_SELF_RELATIVE - This boolean flag, when set, indicates that the
1779  *	security descriptor is in self-relative form.  In this form,
1780  *	all fields of the security descriptor are contiguous in memory
1781  *	and all pointer fields are expressed as offsets from the
1782  *	beginning of the security descriptor.
1783  */
1784 typedef enum {
1785 	SE_OWNER_DEFAULTED		= const_cpu_to_le16(0x0001),
1786 	SE_GROUP_DEFAULTED		= const_cpu_to_le16(0x0002),
1787 	SE_DACL_PRESENT			= const_cpu_to_le16(0x0004),
1788 	SE_DACL_DEFAULTED		= const_cpu_to_le16(0x0008),
1789 	SE_SACL_PRESENT			= const_cpu_to_le16(0x0010),
1790 	SE_SACL_DEFAULTED		= const_cpu_to_le16(0x0020),
1791 	SE_DACL_AUTO_INHERIT_REQ	= const_cpu_to_le16(0x0100),
1792 	SE_SACL_AUTO_INHERIT_REQ	= const_cpu_to_le16(0x0200),
1793 	SE_DACL_AUTO_INHERITED		= const_cpu_to_le16(0x0400),
1794 	SE_SACL_AUTO_INHERITED		= const_cpu_to_le16(0x0800),
1795 	SE_DACL_PROTECTED		= const_cpu_to_le16(0x1000),
1796 	SE_SACL_PROTECTED		= const_cpu_to_le16(0x2000),
1797 	SE_RM_CONTROL_VALID		= const_cpu_to_le16(0x4000),
1798 	SE_SELF_RELATIVE		= const_cpu_to_le16(0x8000),
1799 } __attribute__((__packed__)) SECURITY_DESCRIPTOR_CONTROL;
1800 
1801 /**
1802  * struct SECURITY_DESCRIPTOR_RELATIVE -
1803  *
1804  * Self-relative security descriptor. Contains the owner and group SIDs as well
1805  * as the sacl and dacl ACLs inside the security descriptor itself.
1806  */
1807 typedef struct {
1808 	u8 revision;	/* Revision level of the security descriptor. */
1809 	u8 alignment;
1810 	SECURITY_DESCRIPTOR_CONTROL control; /* Flags qualifying the type of
1811 			   the descriptor as well as the following fields. */
1812 	le32 owner;	/* Byte offset to a SID representing an object's
1813 			   owner. If this is NULL, no owner SID is present in
1814 			   the descriptor. */
1815 	le32 group;	/* Byte offset to a SID representing an object's
1816 			   primary group. If this is NULL, no primary group
1817 			   SID is present in the descriptor. */
1818 	le32 sacl;	/* Byte offset to a system ACL. Only valid, if
1819 			   SE_SACL_PRESENT is set in the control field. If
1820 			   SE_SACL_PRESENT is set but sacl is NULL, a NULL ACL
1821 			   is specified. */
1822 	le32 dacl;	/* Byte offset to a discretionary ACL. Only valid, if
1823 			   SE_DACL_PRESENT is set in the control field. If
1824 			   SE_DACL_PRESENT is set but dacl is NULL, a NULL ACL
1825 			   (unconditionally granting access) is specified. */
1826 /* sizeof() = 0x14 bytes */
1827 } __attribute__((__packed__)) SECURITY_DESCRIPTOR_RELATIVE;
1828 
1829 /**
1830  * struct SECURITY_DESCRIPTOR - Absolute security descriptor.
1831  *
1832  * Does not contain the owner and group SIDs, nor the sacl and dacl ACLs inside
1833  * the security descriptor. Instead, it contains pointers to these structures
1834  * in memory. Obviously, absolute security descriptors are only useful for in
1835  * memory representations of security descriptors.
1836  *
1837  * On disk, a self-relative security descriptor is used.
1838  */
1839 typedef struct {
1840 	u8 revision;	/* Revision level of the security descriptor. */
1841 	u8 alignment;
1842 	SECURITY_DESCRIPTOR_CONTROL control;	/* Flags qualifying the type of
1843 			   the descriptor as well as the following fields. */
1844 	SID *owner;	/* Points to a SID representing an object's owner. If
1845 			   this is NULL, no owner SID is present in the
1846 			   descriptor. */
1847 	SID *group;	/* Points to a SID representing an object's primary
1848 			   group. If this is NULL, no primary group SID is
1849 			   present in the descriptor. */
1850 	ACL *sacl;	/* Points to a system ACL. Only valid, if
1851 			   SE_SACL_PRESENT is set in the control field. If
1852 			   SE_SACL_PRESENT is set but sacl is NULL, a NULL ACL
1853 			   is specified. */
1854 	ACL *dacl;	/* Points to a discretionary ACL. Only valid, if
1855 			   SE_DACL_PRESENT is set in the control field. If
1856 			   SE_DACL_PRESENT is set but dacl is NULL, a NULL ACL
1857 			   (unconditionally granting access) is specified. */
1858 } __attribute__((__packed__)) SECURITY_DESCRIPTOR;
1859 
1860 /**
1861  * enum SECURITY_DESCRIPTOR_CONSTANTS -
1862  *
1863  * Current constants for security descriptors.
1864  */
1865 typedef enum {
1866 	/* Current revision. */
1867 	SECURITY_DESCRIPTOR_REVISION	= 1,
1868 	SECURITY_DESCRIPTOR_REVISION1	= 1,
1869 
1870 	/* The sizes of both the absolute and relative security descriptors is
1871 	   the same as pointers, at least on ia32 architecture are 32-bit. */
1872 	SECURITY_DESCRIPTOR_MIN_LENGTH	= sizeof(SECURITY_DESCRIPTOR),
1873 } SECURITY_DESCRIPTOR_CONSTANTS;
1874 
1875 /*
1876  * Attribute: Security descriptor (0x50).
1877  *
1878  * A standard self-relative security descriptor.
1879  *
1880  * NOTE: Can be resident or non-resident.
1881  * NOTE: Not used in NTFS 3.0+, as security descriptors are stored centrally
1882  * in FILE_Secure and the correct descriptor is found using the security_id
1883  * from the standard information attribute.
1884  */
1885 typedef SECURITY_DESCRIPTOR_RELATIVE SECURITY_DESCRIPTOR_ATTR;
1886 
1887 /*
1888  * On NTFS 3.0+, all security descriptors are stored in FILE_Secure. Only one
1889  * referenced instance of each unique security descriptor is stored.
1890  *
1891  * FILE_Secure contains no unnamed data attribute, i.e. it has zero length. It
1892  * does, however, contain two indexes ($SDH and $SII) as well as a named data
1893  * stream ($SDS).
1894  *
1895  * Every unique security descriptor is assigned a unique security identifier
1896  * (security_id, not to be confused with a SID). The security_id is unique for
1897  * the NTFS volume and is used as an index into the $SII index, which maps
1898  * security_ids to the security descriptor's storage location within the $SDS
1899  * data attribute. The $SII index is sorted by ascending security_id.
1900  *
1901  * A simple hash is computed from each security descriptor. This hash is used
1902  * as an index into the $SDH index, which maps security descriptor hashes to
1903  * the security descriptor's storage location within the $SDS data attribute.
1904  * The $SDH index is sorted by security descriptor hash and is stored in a B+
1905  * tree. When searching $SDH (with the intent of determining whether or not a
1906  * new security descriptor is already present in the $SDS data stream), if a
1907  * matching hash is found, but the security descriptors do not match, the
1908  * search in the $SDH index is continued, searching for a next matching hash.
1909  *
1910  * When a precise match is found, the security_id corresponding to the security
1911  * descriptor in the $SDS attribute is read from the found $SDH index entry and
1912  * is stored in the $STANDARD_INFORMATION attribute of the file/directory to
1913  * which the security descriptor is being applied. The $STANDARD_INFORMATION
1914  * attribute is present in all base mft records (i.e. in all files and
1915  * directories).
1916  *
1917  * If a match is not found, the security descriptor is assigned a new unique
1918  * security_id and is added to the $SDS data attribute. Then, entries
1919  * referencing the this security descriptor in the $SDS data attribute are
1920  * added to the $SDH and $SII indexes.
1921  *
1922  * Note: Entries are never deleted from FILE_Secure, even if nothing
1923  * references an entry any more.
1924  */
1925 
1926 /**
1927  * struct SECURITY_DESCRIPTOR_HEADER -
1928  *
1929  * This header precedes each security descriptor in the $SDS data stream.
1930  * This is also the index entry data part of both the $SII and $SDH indexes.
1931  */
1932 typedef struct {
1933 	le32 hash;	   /* Hash of the security descriptor. */
1934 	le32 security_id;   /* The security_id assigned to the descriptor. */
1935 	le64 offset;	   /* Byte offset of this entry in the $SDS stream. */
1936 	le32 length;	   /* Size in bytes of this entry in $SDS stream. */
1937 } __attribute__((__packed__)) SECURITY_DESCRIPTOR_HEADER;
1938 
1939 /**
1940  * struct SDH_INDEX_DATA -
1941  */
1942 typedef struct {
1943 	le32 hash;          /* Hash of the security descriptor. */
1944 	le32 security_id;   /* The security_id assigned to the descriptor. */
1945 	le64 offset;	   /* Byte offset of this entry in the $SDS stream. */
1946 	le32 length;	   /* Size in bytes of this entry in $SDS stream. */
1947 	le32 reserved_II;   /* Padding - always unicode "II" or zero. This field
1948 			      isn't counted in INDEX_ENTRY's data_length. */
1949 } __attribute__((__packed__)) SDH_INDEX_DATA;
1950 
1951 /**
1952  * struct SII_INDEX_DATA -
1953  */
1954 typedef SECURITY_DESCRIPTOR_HEADER SII_INDEX_DATA;
1955 
1956 /**
1957  * struct SDS_ENTRY -
1958  *
1959  * The $SDS data stream contains the security descriptors, aligned on 16-byte
1960  * boundaries, sorted by security_id in a B+ tree. Security descriptors cannot
1961  * cross 256kib boundaries (this restriction is imposed by the Windows cache
1962  * manager). Each security descriptor is contained in a SDS_ENTRY structure.
1963  * Also, each security descriptor is stored twice in the $SDS stream with a
1964  * fixed offset of 0x40000 bytes (256kib, the Windows cache manager's max size)
1965  * between them; i.e. if a SDS_ENTRY specifies an offset of 0x51d0, then the
1966  * the first copy of the security descriptor will be at offset 0x51d0 in the
1967  * $SDS data stream and the second copy will be at offset 0x451d0.
1968  */
1969 typedef struct {
1970 /*  0	SECURITY_DESCRIPTOR_HEADER; -- Unfolded here as gcc doesn't like
1971 				       unnamed structs. */
1972 	le32 hash;	   /* Hash of the security descriptor. */
1973 	le32 security_id;   /* The security_id assigned to the descriptor. */
1974 	le64 offset;	   /* Byte offset of this entry in the $SDS stream. */
1975 	le32 length;	   /* Size in bytes of this entry in $SDS stream. */
1976 /* 20*/	SECURITY_DESCRIPTOR_RELATIVE sid; /* The self-relative security
1977 					     descriptor. */
1978 } __attribute__((__packed__)) SDS_ENTRY;
1979 
1980 /**
1981  * struct SII_INDEX_KEY - The index entry key used in the $SII index.
1982  *
1983  * The collation type is COLLATION_NTOFS_ULONG.
1984  */
1985 typedef struct {
1986 	le32 security_id;   /* The security_id assigned to the descriptor. */
1987 } __attribute__((__packed__)) SII_INDEX_KEY;
1988 
1989 /**
1990  * struct SDH_INDEX_KEY - The index entry key used in the $SDH index.
1991  *
1992  * The keys are sorted first by hash and then by security_id.
1993  * The collation rule is COLLATION_NTOFS_SECURITY_HASH.
1994  */
1995 typedef struct {
1996 	le32 hash;	   /* Hash of the security descriptor. */
1997 	le32 security_id;   /* The security_id assigned to the descriptor. */
1998 } __attribute__((__packed__)) SDH_INDEX_KEY;
1999 
2000 /**
2001  * struct VOLUME_NAME - Attribute: Volume name (0x60).
2002  *
2003  * NOTE: Always resident.
2004  * NOTE: Present only in FILE_Volume.
2005  */
2006 typedef struct {
2007 	ntfschar name[0];	/* The name of the volume in Unicode. */
2008 } __attribute__((__packed__)) VOLUME_NAME;
2009 
2010 /**
2011  * enum VOLUME_FLAGS - Possible flags for the volume (16-bit).
2012  */
2013 typedef enum {
2014 	VOLUME_IS_DIRTY			= const_cpu_to_le16(0x0001),
2015 	VOLUME_RESIZE_LOG_FILE		= const_cpu_to_le16(0x0002),
2016 	VOLUME_UPGRADE_ON_MOUNT		= const_cpu_to_le16(0x0004),
2017 	VOLUME_MOUNTED_ON_NT4		= const_cpu_to_le16(0x0008),
2018 	VOLUME_DELETE_USN_UNDERWAY	= const_cpu_to_le16(0x0010),
2019 	VOLUME_REPAIR_OBJECT_ID		= const_cpu_to_le16(0x0020),
2020 	VOLUME_CHKDSK_UNDERWAY		= const_cpu_to_le16(0x4000),
2021 	VOLUME_MODIFIED_BY_CHKDSK	= const_cpu_to_le16(0x8000),
2022 	VOLUME_FLAGS_MASK		= const_cpu_to_le16(0xc03f),
2023 } __attribute__((__packed__)) VOLUME_FLAGS;
2024 
2025 /**
2026  * struct VOLUME_INFORMATION - Attribute: Volume information (0x70).
2027  *
2028  * NOTE: Always resident.
2029  * NOTE: Present only in FILE_Volume.
2030  * NOTE: Windows 2000 uses NTFS 3.0 while Windows NT4 service pack 6a uses
2031  *	 NTFS 1.2. I haven't personally seen other values yet.
2032  */
2033 typedef struct {
2034 	le64 reserved;		/* Not used (yet?). */
2035 	u8 major_ver;		/* Major version of the ntfs format. */
2036 	u8 minor_ver;		/* Minor version of the ntfs format. */
2037 	VOLUME_FLAGS flags;	/* Bit array of VOLUME_* flags. */
2038 } __attribute__((__packed__)) VOLUME_INFORMATION;
2039 
2040 /**
2041  * struct DATA_ATTR - Attribute: Data attribute (0x80).
2042  *
2043  * NOTE: Can be resident or non-resident.
2044  *
2045  * Data contents of a file (i.e. the unnamed stream) or of a named stream.
2046  */
2047 typedef struct {
2048 	u8 data[0];		/* The file's data contents. */
2049 } __attribute__((__packed__)) DATA_ATTR;
2050 
2051 /**
2052  * enum INDEX_HEADER_FLAGS - Index header flags (8-bit).
2053  */
2054 typedef enum {
2055 	/* When index header is in an index root attribute: */
2056 	SMALL_INDEX	= 0, /* The index is small enough to fit inside the
2057 				index root attribute and there is no index
2058 				allocation attribute present. */
2059 	LARGE_INDEX	= 1, /* The index is too large to fit in the index
2060 				root attribute and/or an index allocation
2061 				attribute is present. */
2062 	/*
2063 	 * When index header is in an index block, i.e. is part of index
2064 	 * allocation attribute:
2065 	 */
2066 	LEAF_NODE	= 0, /* This is a leaf node, i.e. there are no more
2067 				nodes branching off it. */
2068 	INDEX_NODE	= 1, /* This node indexes other nodes, i.e. is not a
2069 				leaf node. */
2070 	NODE_MASK	= 1, /* Mask for accessing the *_NODE bits. */
2071 } __attribute__((__packed__)) INDEX_HEADER_FLAGS;
2072 
2073 /**
2074  * struct INDEX_HEADER -
2075  *
2076  * This is the header for indexes, describing the INDEX_ENTRY records, which
2077  * follow the INDEX_HEADER. Together the index header and the index entries
2078  * make up a complete index.
2079  *
2080  * IMPORTANT NOTE: The offset, length and size structure members are counted
2081  * relative to the start of the index header structure and not relative to the
2082  * start of the index root or index allocation structures themselves.
2083  */
2084 typedef struct {
2085 /*  0*/	le32 entries_offset;	/* Byte offset from the INDEX_HEADER to first
2086 				   INDEX_ENTRY, aligned to 8-byte boundary.  */
2087 /*  4*/	le32 index_length;	/* Data size in byte of the INDEX_ENTRY's,
2088 				   including the INDEX_HEADER, aligned to 8. */
2089 /*  8*/	le32 allocated_size;	/* Allocated byte size of this index (block),
2090 				   multiple of 8 bytes. See more below.      */
2091 	/*
2092 	   For the index root attribute, the above two numbers are always
2093 	   equal, as the attribute is resident and it is resized as needed.
2094 
2095 	   For the index allocation attribute, the attribute is not resident
2096 	   and the allocated_size is equal to the index_block_size specified
2097 	   by the corresponding INDEX_ROOT attribute minus the INDEX_BLOCK
2098 	   size not counting the INDEX_HEADER part (i.e. minus -24).
2099 	 */
2100 /* 12*/	INDEX_HEADER_FLAGS ih_flags;	/* Bit field of INDEX_HEADER_FLAGS.  */
2101 /* 13*/	u8 reserved[3];			/* Reserved/align to 8-byte boundary.*/
2102 /* sizeof() == 16 */
2103 } __attribute__((__packed__)) INDEX_HEADER;
2104 
2105 /**
2106  * struct INDEX_ROOT - Attribute: Index root (0x90).
2107  *
2108  * NOTE: Always resident.
2109  *
2110  * This is followed by a sequence of index entries (INDEX_ENTRY structures)
2111  * as described by the index header.
2112  *
2113  * When a directory is small enough to fit inside the index root then this
2114  * is the only attribute describing the directory. When the directory is too
2115  * large to fit in the index root, on the other hand, two additional attributes
2116  * are present: an index allocation attribute, containing sub-nodes of the B+
2117  * directory tree (see below), and a bitmap attribute, describing which virtual
2118  * cluster numbers (vcns) in the index allocation attribute are in use by an
2119  * index block.
2120  *
2121  * NOTE: The root directory (FILE_root) contains an entry for itself. Other
2122  * directories do not contain entries for themselves, though.
2123  */
2124 typedef struct {
2125 /*  0*/	ATTR_TYPES type;		/* Type of the indexed attribute. Is
2126 					   $FILE_NAME for directories, zero
2127 					   for view indexes. No other values
2128 					   allowed. */
2129 /*  4*/	COLLATION_RULES collation_rule;	/* Collation rule used to sort the
2130 					   index entries. If type is $FILE_NAME,
2131 					   this must be COLLATION_FILE_NAME. */
2132 /*  8*/	le32 index_block_size;		/* Size of index block in bytes (in
2133 					   the index allocation attribute). */
2134 /* 12*/	s8 clusters_per_index_block;	/* Size of index block in clusters (in
2135 					   the index allocation attribute), when
2136 					   an index block is >= than a cluster,
2137 					   otherwise sectors per index block. */
2138 /* 13*/	u8 reserved[3];			/* Reserved/align to 8-byte boundary. */
2139 /* 16*/	INDEX_HEADER index;		/* Index header describing the
2140 					   following index entries. */
2141 /* sizeof()= 32 bytes */
2142 } __attribute__((__packed__)) INDEX_ROOT;
2143 
2144 /**
2145  * struct INDEX_BLOCK - Attribute: Index allocation (0xa0).
2146  *
2147  * NOTE: Always non-resident (doesn't make sense to be resident anyway!).
2148  *
2149  * This is an array of index blocks. Each index block starts with an
2150  * INDEX_BLOCK structure containing an index header, followed by a sequence of
2151  * index entries (INDEX_ENTRY structures), as described by the INDEX_HEADER.
2152  */
2153 typedef struct {
2154 /*  0	NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */
2155 	NTFS_RECORD_TYPES magic;/* Magic is "INDX". */
2156 	le16 usa_ofs;		/* See NTFS_RECORD definition. */
2157 	le16 usa_count;		/* See NTFS_RECORD definition. */
2158 
2159 /*  8*/	leLSN lsn;		/* $LogFile sequence number of the last
2160 				   modification of this index block. */
2161 /* 16*/	leVCN index_block_vcn;	/* Virtual cluster number of the index block. */
2162 /* 24*/	INDEX_HEADER index;	/* Describes the following index entries. */
2163 /* sizeof()= 40 (0x28) bytes */
2164 /*
2165  * When creating the index block, we place the update sequence array at this
2166  * offset, i.e. before we start with the index entries. This also makes sense,
2167  * otherwise we could run into problems with the update sequence array
2168  * containing in itself the last two bytes of a sector which would mean that
2169  * multi sector transfer protection wouldn't work. As you can't protect data
2170  * by overwriting it since you then can't get it back...
2171  * When reading use the data from the ntfs record header.
2172  */
2173 } __attribute__((__packed__)) INDEX_BLOCK;
2174 
2175 typedef INDEX_BLOCK INDEX_ALLOCATION;
2176 
2177 /**
2178  * struct REPARSE_INDEX_KEY -
2179  *
2180  * The system file FILE_Extend/$Reparse contains an index named $R listing
2181  * all reparse points on the volume. The index entry keys are as defined
2182  * below. Note, that there is no index data associated with the index entries.
2183  *
2184  * The index entries are sorted by the index key file_id. The collation rule is
2185  * COLLATION_NTOFS_ULONGS. FIXME: Verify whether the reparse_tag is not the
2186  * primary key / is not a key at all. (AIA)
2187  */
2188 typedef struct {
2189 	le32 reparse_tag;	/* Reparse point type (inc. flags). */
2190 	leMFT_REF file_id;	/* Mft record of the file containing the
2191 				   reparse point attribute. */
2192 } __attribute__((__packed__)) REPARSE_INDEX_KEY;
2193 
2194 /**
2195  * enum QUOTA_FLAGS - Quota flags (32-bit).
2196  */
2197 typedef enum {
2198 	/* The user quota flags. Names explain meaning. */
2199 	QUOTA_FLAG_DEFAULT_LIMITS	= const_cpu_to_le32(0x00000001),
2200 	QUOTA_FLAG_LIMIT_REACHED	= const_cpu_to_le32(0x00000002),
2201 	QUOTA_FLAG_ID_DELETED		= const_cpu_to_le32(0x00000004),
2202 
2203 	QUOTA_FLAG_USER_MASK		= const_cpu_to_le32(0x00000007),
2204 		/* Bit mask for user quota flags. */
2205 
2206 	/* These flags are only present in the quota defaults index entry,
2207 	   i.e. in the entry where owner_id = QUOTA_DEFAULTS_ID. */
2208 	QUOTA_FLAG_TRACKING_ENABLED	= const_cpu_to_le32(0x00000010),
2209 	QUOTA_FLAG_ENFORCEMENT_ENABLED	= const_cpu_to_le32(0x00000020),
2210 	QUOTA_FLAG_TRACKING_REQUESTED	= const_cpu_to_le32(0x00000040),
2211 	QUOTA_FLAG_LOG_THRESHOLD	= const_cpu_to_le32(0x00000080),
2212 	QUOTA_FLAG_LOG_LIMIT		= const_cpu_to_le32(0x00000100),
2213 	QUOTA_FLAG_OUT_OF_DATE		= const_cpu_to_le32(0x00000200),
2214 	QUOTA_FLAG_CORRUPT		= const_cpu_to_le32(0x00000400),
2215 	QUOTA_FLAG_PENDING_DELETES	= const_cpu_to_le32(0x00000800),
2216 } QUOTA_FLAGS;
2217 
2218 /**
2219  * struct QUOTA_CONTROL_ENTRY -
2220  *
2221  * The system file FILE_Extend/$Quota contains two indexes $O and $Q. Quotas
2222  * are on a per volume and per user basis.
2223  *
2224  * The $Q index contains one entry for each existing user_id on the volume. The
2225  * index key is the user_id of the user/group owning this quota control entry,
2226  * i.e. the key is the owner_id. The user_id of the owner of a file, i.e. the
2227  * owner_id, is found in the standard information attribute. The collation rule
2228  * for $Q is COLLATION_NTOFS_ULONG.
2229  *
2230  * The $O index contains one entry for each user/group who has been assigned
2231  * a quota on that volume. The index key holds the SID of the user_id the
2232  * entry belongs to, i.e. the owner_id. The collation rule for $O is
2233  * COLLATION_NTOFS_SID.
2234  *
2235  * The $O index entry data is the user_id of the user corresponding to the SID.
2236  * This user_id is used as an index into $Q to find the quota control entry
2237  * associated with the SID.
2238  *
2239  * The $Q index entry data is the quota control entry and is defined below.
2240  */
2241 typedef struct {
2242 	le32 version;		/* Currently equals 2. */
2243 	QUOTA_FLAGS flags;	/* Flags describing this quota entry. */
2244 	le64 bytes_used;		/* How many bytes of the quota are in use. */
2245 	sle64 change_time;	/* Last time this quota entry was changed. */
2246 	sle64 threshold;		/* Soft quota (-1 if not limited). */
2247 	sle64 limit;		/* Hard quota (-1 if not limited). */
2248 	sle64 exceeded_time;	/* How long the soft quota has been exceeded. */
2249 /* The below field is NOT present for the quota defaults entry. */
2250 	SID sid;		/* The SID of the user/object associated with
2251 				   this quota entry. If this field is missing
2252 				   then the INDEX_ENTRY is padded to a multiple
2253 				   of 8 with zeros which are not counted in
2254 				   the data_length field. If the sid is present
2255 				   then this structure is padded with zeros to
2256 				   a multiple of 8 and the padding is counted in
2257 				   the INDEX_ENTRY's data_length. */
2258 } __attribute__((__packed__)) QUOTA_CONTROL_ENTRY;
2259 
2260 /**
2261  * struct QUOTA_O_INDEX_DATA -
2262  */
2263 typedef struct {
2264 	le32 owner_id;
2265 	le32 unknown;		/* Always 32. Seems to be padding and it's not
2266 				   counted in the INDEX_ENTRY's data_length.
2267 				   This field shouldn't be really here. */
2268 } __attribute__((__packed__)) QUOTA_O_INDEX_DATA;
2269 
2270 /**
2271  * enum PREDEFINED_OWNER_IDS - Predefined owner_id values (32-bit).
2272  */
2273 typedef enum {
2274 	QUOTA_INVALID_ID	= const_cpu_to_le32(0x00000000),
2275 	QUOTA_DEFAULTS_ID	= const_cpu_to_le32(0x00000001),
2276 	QUOTA_FIRST_USER_ID	= const_cpu_to_le32(0x00000100),
2277 } PREDEFINED_OWNER_IDS;
2278 
2279 /**
2280  * enum INDEX_ENTRY_FLAGS - Index entry flags (16-bit).
2281  */
2282 typedef enum {
2283 	INDEX_ENTRY_NODE = const_cpu_to_le16(1), /* This entry contains a
2284 					sub-node, i.e. a reference to an index
2285 					block in form of a virtual cluster
2286 					number (see below). */
2287 	INDEX_ENTRY_END  = const_cpu_to_le16(2), /* This signifies the last
2288 					entry in an index block. The index
2289 					entry does not represent a file but it
2290 					can point to a sub-node. */
2291 	INDEX_ENTRY_SPACE_FILLER = 0xffff, /* Just to force 16-bit width. */
2292 } __attribute__((__packed__)) INDEX_ENTRY_FLAGS;
2293 
2294 /**
2295  * struct INDEX_ENTRY_HEADER - This the index entry header (see below).
2296  *
2297  *         ==========================================================
2298  *         !!!!!  SEE DESCRIPTION OF THE FIELDS AT INDEX_ENTRY  !!!!!
2299  *         ==========================================================
2300  */
2301 typedef struct {
2302 /*  0*/	union {
2303 		leMFT_REF indexed_file;
2304 		struct {
2305 			le16 data_offset;
2306 			le16 data_length;
2307 			le32 reservedV;
2308 		} __attribute__((__packed__));
2309 	} __attribute__((__packed__));
2310 /*  8*/	le16 length;
2311 /* 10*/	le16 key_length;
2312 /* 12*/	INDEX_ENTRY_FLAGS flags;
2313 /* 14*/	le16 reserved;
2314 /* sizeof() = 16 bytes */
2315 } __attribute__((__packed__)) INDEX_ENTRY_HEADER;
2316 
2317 /**
2318  * struct INDEX_ENTRY - This is an index entry.
2319  *
2320  * A sequence of such entries follows each INDEX_HEADER structure. Together
2321  * they make up a complete index. The index follows either an index root
2322  * attribute or an index allocation attribute.
2323  *
2324  * NOTE: Before NTFS 3.0 only filename attributes were indexed.
2325  */
2326 typedef struct {
2327 /*  0	INDEX_ENTRY_HEADER; -- Unfolded here as gcc dislikes unnamed structs. */
2328 	union {		/* Only valid when INDEX_ENTRY_END is not set. */
2329 		leMFT_REF indexed_file;		/* The mft reference of the file
2330 						   described by this index
2331 						   entry. Used for directory
2332 						   indexes. */
2333 		struct { /* Used for views/indexes to find the entry's data. */
2334 			le16 data_offset;	/* Data byte offset from this
2335 						   INDEX_ENTRY. Follows the
2336 						   index key. */
2337 			le16 data_length;	/* Data length in bytes. */
2338 			le32 reservedV;		/* Reserved (zero). */
2339 		} __attribute__((__packed__));
2340 	} __attribute__((__packed__));
2341 /*  8*/ le16 length;		 /* Byte size of this index entry, multiple of
2342 				    8-bytes. Size includes INDEX_ENTRY_HEADER
2343 				    and the optional subnode VCN. See below. */
2344 /* 10*/ le16 key_length;		 /* Byte size of the key value, which is in the
2345 				    index entry. It follows field reserved. Not
2346 				    multiple of 8-bytes. */
2347 /* 12*/	INDEX_ENTRY_FLAGS ie_flags; /* Bit field of INDEX_ENTRY_* flags. */
2348 /* 14*/	le16 reserved;		 /* Reserved/align to 8-byte boundary. */
2349 /*	End of INDEX_ENTRY_HEADER */
2350 /* 16*/	union {		/* The key of the indexed attribute. NOTE: Only present
2351 			   if INDEX_ENTRY_END bit in flags is not set. NOTE: On
2352 			   NTFS versions before 3.0 the only valid key is the
2353 			   FILE_NAME_ATTR. On NTFS 3.0+ the following
2354 			   additional index keys are defined: */
2355 		FILE_NAME_ATTR file_name;/* $I30 index in directories. */
2356 		SII_INDEX_KEY sii;	/* $SII index in $Secure. */
2357 		SDH_INDEX_KEY sdh;	/* $SDH index in $Secure. */
2358 		GUID object_id;		/* $O index in FILE_Extend/$ObjId: The
2359 					   object_id of the mft record found in
2360 					   the data part of the index. */
2361 		REPARSE_INDEX_KEY reparse;	/* $R index in
2362 						   FILE_Extend/$Reparse. */
2363 		SID sid;		/* $O index in FILE_Extend/$Quota:
2364 					   SID of the owner of the user_id. */
2365 		le32 owner_id;		/* $Q index in FILE_Extend/$Quota:
2366 					   user_id of the owner of the quota
2367 					   control entry in the data part of
2368 					   the index. */
2369 	} __attribute__((__packed__)) key;
2370 	/* The (optional) index data is inserted here when creating.
2371 	leVCN vcn;	   If INDEX_ENTRY_NODE bit in ie_flags is set, the last
2372 			   eight bytes of this index entry contain the virtual
2373 			   cluster number of the index block that holds the
2374 			   entries immediately preceding the current entry.
2375 
2376 			   If the key_length is zero, then the vcn immediately
2377 			   follows the INDEX_ENTRY_HEADER.
2378 
2379 			   The address of the vcn of "ie" INDEX_ENTRY is given by
2380 			   (char*)ie + le16_to_cpu(ie->length) - sizeof(VCN)
2381 	*/
2382 } __attribute__((__packed__)) INDEX_ENTRY;
2383 
2384 /**
2385  * struct BITMAP_ATTR - Attribute: Bitmap (0xb0).
2386  *
2387  * Contains an array of bits (aka a bitfield).
2388  *
2389  * When used in conjunction with the index allocation attribute, each bit
2390  * corresponds to one index block within the index allocation attribute. Thus
2391  * the number of bits in the bitmap * index block size / cluster size is the
2392  * number of clusters in the index allocation attribute.
2393  */
2394 typedef struct {
2395 	u8 bitmap[0];			/* Array of bits. */
2396 } __attribute__((__packed__)) BITMAP_ATTR;
2397 
2398 /**
2399  * enum PREDEFINED_REPARSE_TAGS -
2400  *
2401  * The reparse point tag defines the type of the reparse point. It also
2402  * includes several flags, which further describe the reparse point.
2403  *
2404  * The reparse point tag is an unsigned 32-bit value divided in three parts:
2405  *
2406  * 1. The least significant 16 bits (i.e. bits 0 to 15) specify the type of
2407  *    the reparse point.
2408  * 2. The 12 bits after this (i.e. bits 16 to 27) are reserved for future use.
2409  * 3. The most significant four bits are flags describing the reparse point.
2410  *    They are defined as follows:
2411  *	bit 28: Directory bit. If set, the directory is not a surrogate
2412  *		and can be used the usual way.
2413  *	bit 29: Name surrogate bit. If set, the filename is an alias for
2414  *		another object in the system.
2415  *	bit 30: High-latency bit. If set, accessing the first byte of data will
2416  *		be slow. (E.g. the data is stored on a tape drive.)
2417  *	bit 31: Microsoft bit. If set, the tag is owned by Microsoft. User
2418  *		defined tags have to use zero here.
2419  * 4. Moreover, on Windows 10 :
2420  *	Some flags may be used in bits 12 to 15 to further describe the
2421  *	reparse point.
2422  */
2423 typedef enum {
2424 	IO_REPARSE_TAG_DIRECTORY	= const_cpu_to_le32(0x10000000),
2425 	IO_REPARSE_TAG_IS_ALIAS		= const_cpu_to_le32(0x20000000),
2426 	IO_REPARSE_TAG_IS_HIGH_LATENCY	= const_cpu_to_le32(0x40000000),
2427 	IO_REPARSE_TAG_IS_MICROSOFT	= const_cpu_to_le32(0x80000000),
2428 
2429 	IO_REPARSE_TAG_RESERVED_ZERO	= const_cpu_to_le32(0x00000000),
2430 	IO_REPARSE_TAG_RESERVED_ONE	= const_cpu_to_le32(0x00000001),
2431 	IO_REPARSE_TAG_RESERVED_RANGE	= const_cpu_to_le32(0x00000001),
2432 
2433 	IO_REPARSE_TAG_CSV		= const_cpu_to_le32(0x80000009),
2434 	IO_REPARSE_TAG_DEDUP		= const_cpu_to_le32(0x80000013),
2435 	IO_REPARSE_TAG_DFS		= const_cpu_to_le32(0x8000000A),
2436 	IO_REPARSE_TAG_DFSR		= const_cpu_to_le32(0x80000012),
2437 	IO_REPARSE_TAG_HSM		= const_cpu_to_le32(0xC0000004),
2438 	IO_REPARSE_TAG_HSM2		= const_cpu_to_le32(0x80000006),
2439 	IO_REPARSE_TAG_MOUNT_POINT	= const_cpu_to_le32(0xA0000003),
2440 	IO_REPARSE_TAG_NFS		= const_cpu_to_le32(0x80000014),
2441 	IO_REPARSE_TAG_SIS		= const_cpu_to_le32(0x80000007),
2442 	IO_REPARSE_TAG_SYMLINK		= const_cpu_to_le32(0xA000000C),
2443 	IO_REPARSE_TAG_WIM		= const_cpu_to_le32(0x80000008),
2444 	IO_REPARSE_TAG_DFM		= const_cpu_to_le32(0x80000016),
2445 	IO_REPARSE_TAG_WOF		= const_cpu_to_le32(0x80000017),
2446 	IO_REPARSE_TAG_WCI		= const_cpu_to_le32(0x80000018),
2447 	IO_REPARSE_TAG_CLOUD		= const_cpu_to_le32(0x9000001A),
2448 	IO_REPARSE_TAG_APPEXECLINK	= const_cpu_to_le32(0x8000001B),
2449 	IO_REPARSE_TAG_GVFS		= const_cpu_to_le32(0x9000001C),
2450 	IO_REPARSE_TAG_LX_SYMLINK	= const_cpu_to_le32(0xA000001D),
2451 	IO_REPARSE_TAG_AF_UNIX		= const_cpu_to_le32(0x80000023),
2452 	IO_REPARSE_TAG_LX_FIFO		= const_cpu_to_le32(0x80000024),
2453 	IO_REPARSE_TAG_LX_CHR		= const_cpu_to_le32(0x80000025),
2454 	IO_REPARSE_TAG_LX_BLK		= const_cpu_to_le32(0x80000026),
2455 
2456 	IO_REPARSE_TAG_VALID_VALUES	= const_cpu_to_le32(0xf000ffff),
2457 	IO_REPARSE_PLUGIN_SELECT	= const_cpu_to_le32(0xffff0fff),
2458 } PREDEFINED_REPARSE_TAGS;
2459 
2460 /**
2461  * struct REPARSE_POINT - Attribute: Reparse point (0xc0).
2462  *
2463  * NOTE: Can be resident or non-resident.
2464  */
2465 typedef struct {
2466 	le32 reparse_tag;		/* Reparse point type (inc. flags). */
2467 	le16 reparse_data_length;	/* Byte size of reparse data. */
2468 	le16 reserved;			/* Align to 8-byte boundary. */
2469 	u8 reparse_data[0];		/* Meaning depends on reparse_tag. */
2470 } __attribute__((__packed__)) REPARSE_POINT;
2471 
2472 /**
2473  * struct EA_INFORMATION - Attribute: Extended attribute information (0xd0).
2474  *
2475  * NOTE: Always resident.
2476  */
2477 typedef struct {
2478 	le16 ea_length;		/* Byte size of the packed extended
2479 				   attributes. */
2480 	le16 need_ea_count;	/* The number of extended attributes which have
2481 				   the NEED_EA bit set. */
2482 	le32 ea_query_length;	/* Byte size of the buffer required to query
2483 				   the extended attributes when calling
2484 				   ZwQueryEaFile() in Windows NT/2k. I.e. the
2485 				   byte size of the unpacked extended
2486 				   attributes. */
2487 } __attribute__((__packed__)) EA_INFORMATION;
2488 
2489 /**
2490  * enum EA_FLAGS - Extended attribute flags (8-bit).
2491  */
2492 typedef enum {
2493 	NEED_EA	= 0x80,		/* Indicate that the file to which the EA
2494 				   belongs cannot be interpreted without
2495 				   understanding the associated extended
2496 				   attributes. */
2497 } __attribute__((__packed__)) EA_FLAGS;
2498 
2499 /**
2500  * struct EA_ATTR - Attribute: Extended attribute (EA) (0xe0).
2501  *
2502  * Like the attribute list and the index buffer list, the EA attribute value is
2503  * a sequence of EA_ATTR variable length records.
2504  *
2505  * FIXME: It appears weird that the EA name is not Unicode. Is it true?
2506  * FIXME: It seems that name is always uppercased. Is it true?
2507  */
2508 typedef struct {
2509 	le32 next_entry_offset;	/* Offset to the next EA_ATTR. */
2510 	EA_FLAGS flags;		/* Flags describing the EA. */
2511 	u8 name_length;		/* Length of the name of the extended
2512 				   attribute in bytes. */
2513 	le16 value_length;	/* Byte size of the EA's value. */
2514 	u8 name[0];		/* Name of the EA. */
2515 	u8 value[0];		/* The value of the EA. Immediately
2516 				   follows the name. */
2517 } __attribute__((__packed__)) EA_ATTR;
2518 
2519 /**
2520  * struct PROPERTY_SET - Attribute: Property set (0xf0).
2521  *
2522  * Intended to support Native Structure Storage (NSS) - a feature removed from
2523  * NTFS 3.0 during beta testing.
2524  */
2525 typedef struct {
2526 	/* Irrelevant as feature unused. */
2527 } __attribute__((__packed__)) PROPERTY_SET;
2528 
2529 /**
2530  * struct LOGGED_UTILITY_STREAM - Attribute: Logged utility stream (0x100).
2531  *
2532  * NOTE: Can be resident or non-resident.
2533  *
2534  * Operations on this attribute are logged to the journal ($LogFile) like
2535  * normal metadata changes.
2536  *
2537  * Used by the Encrypting File System (EFS).  All encrypted files have this
2538  * attribute with the name $EFS.  See below for the relevant structures.
2539  */
2540 typedef struct {
2541 	/* Can be anything the creator chooses. */
2542 } __attribute__((__packed__)) LOGGED_UTILITY_STREAM;
2543 
2544 /*
2545  * $EFS Data Structure:
2546  *
2547  * The following information is about the data structures that are contained
2548  * inside a logged utility stream (0x100) with a name of "$EFS".
2549  *
2550  * The stream starts with an instance of EFS_ATTR_HEADER.
2551  *
2552  * Next, at offsets offset_to_ddf_array and offset_to_drf_array (unless any of
2553  * them is 0) there is a EFS_DF_ARRAY_HEADER immediately followed by a sequence
2554  * of multiple data decryption/recovery fields.
2555  *
2556  * Each data decryption/recovery field starts with a EFS_DF_HEADER and the next
2557  * one (if it exists) can be found by adding EFS_DF_HEADER->df_length bytes to
2558  * the offset of the beginning of the current EFS_DF_HEADER.
2559  *
2560  * The data decryption/recovery field contains an EFS_DF_CERTIFICATE_HEADER, a
2561  * SID, an optional GUID, an optional container name, a non-optional user name,
2562  * and the encrypted FEK.
2563  *
2564  * Note all the below are best guesses so may have mistakes/inaccuracies.
2565  * Corrections/clarifications/additions are always welcome!
2566  *
2567  * Ntfs.sys takes an EFS value length of <= 0x54 or > 0x40000 to BSOD, i.e. it
2568  * is invalid.
2569  */
2570 
2571 /**
2572  * struct EFS_ATTR_HEADER - "$EFS" header.
2573  *
2574  * The header of the Logged utility stream (0x100) attribute named "$EFS".
2575  */
2576 typedef struct {
2577 /*  0*/	le32 length;		/* Length of EFS attribute in bytes. */
2578 	le32 state;		/* Always 0? */
2579 	le32 version;		/* Efs version.  Always 2? */
2580 	le32 crypto_api_version;	/* Always 0? */
2581 /* 16*/	u8 unknown4[16];	/* MD5 hash of decrypted FEK?  This field is
2582 				   created with a call to UuidCreate() so is
2583 				   unlikely to be an MD5 hash and is more
2584 				   likely to be GUID of this encrytped file
2585 				   or something like that. */
2586 /* 32*/	u8 unknown5[16];	/* MD5 hash of DDFs? */
2587 /* 48*/	u8 unknown6[16];	/* MD5 hash of DRFs? */
2588 /* 64*/	le32 offset_to_ddf_array;/* Offset in bytes to the array of data
2589 				   decryption fields (DDF), see below.  Zero if
2590 				   no DDFs are present. */
2591 	le32 offset_to_drf_array;/* Offset in bytes to the array of data
2592 				   recovery fields (DRF), see below.  Zero if
2593 				   no DRFs are present. */
2594 	le32 reserved;		/* Reserved. */
2595 } __attribute__((__packed__)) EFS_ATTR_HEADER;
2596 
2597 /**
2598  * struct EFS_DF_ARRAY_HEADER -
2599  */
2600 typedef struct {
2601 	le32 df_count;		/* Number of data decryption/recovery fields in
2602 				   the array. */
2603 } __attribute__((__packed__)) EFS_DF_ARRAY_HEADER;
2604 
2605 /**
2606  * struct EFS_DF_HEADER -
2607  */
2608 typedef struct {
2609 /*  0*/	le32 df_length;		/* Length of this data decryption/recovery
2610 				   field in bytes. */
2611 	le32 cred_header_offset;	/* Offset in bytes to the credential header. */
2612 	le32 fek_size;		/* Size in bytes of the encrypted file
2613 				   encryption key (FEK). */
2614 	le32 fek_offset;		/* Offset in bytes to the FEK from the start of
2615 				   the data decryption/recovery field. */
2616 /* 16*/	le32 unknown1;		/* always 0?  Might be just padding. */
2617 } __attribute__((__packed__)) EFS_DF_HEADER;
2618 
2619 /**
2620  * struct EFS_DF_CREDENTIAL_HEADER -
2621  */
2622 typedef struct {
2623 /*  0*/	le32 cred_length;	/* Length of this credential in bytes. */
2624 	le32 sid_offset;		/* Offset in bytes to the user's sid from start
2625 				   of this structure.  Zero if no sid is
2626 				   present. */
2627 /*  8*/	le32 type;		/* Type of this credential:
2628 					1 = CryptoAPI container.
2629 					2 = Unexpected type.
2630 					3 = Certificate thumbprint.
2631 					other = Unknown type. */
2632 	union {
2633 		/* CryptoAPI container. */
2634 		struct {
2635 /* 12*/			le32 container_name_offset;	/* Offset in bytes to
2636 				   the name of the container from start of this
2637 				   structure (may not be zero). */
2638 /* 16*/			le32 provider_name_offset;	/* Offset in bytes to
2639 				   the name of the provider from start of this
2640 				   structure (may not be zero). */
2641 			le32 public_key_blob_offset;	/* Offset in bytes to
2642 				   the public key blob from start of this
2643 				   structure. */
2644 /* 24*/			le32 public_key_blob_size;	/* Size in bytes of
2645 				   public key blob. */
2646 		} __attribute__((__packed__));
2647 		/* Certificate thumbprint. */
2648 		struct {
2649 /* 12*/			le32 cert_thumbprint_header_size;	/* Size in
2650 				   bytes of the header of the certificate
2651 				   thumbprint. */
2652 /* 16*/			le32 cert_thumbprint_header_offset;	/* Offset in
2653 				   bytes to the header of the certificate
2654 				   thumbprint from start of this structure. */
2655 			le32 unknown1;	/* Always 0?  Might be padding... */
2656 			le32 unknown2;	/* Always 0?  Might be padding... */
2657 		} __attribute__((__packed__));
2658 	} __attribute__((__packed__));
2659 } __attribute__((__packed__)) EFS_DF_CREDENTIAL_HEADER;
2660 
2661 typedef EFS_DF_CREDENTIAL_HEADER EFS_DF_CRED_HEADER;
2662 
2663 /**
2664  * struct EFS_DF_CERTIFICATE_THUMBPRINT_HEADER -
2665  */
2666 typedef struct {
2667 /*  0*/	le32 thumbprint_offset;		/* Offset in bytes to the thumbprint. */
2668 	le32 thumbprint_size;		/* Size of thumbprint in bytes. */
2669 /*  8*/	le32 container_name_offset;	/* Offset in bytes to the name of the
2670 					   container from start of this
2671 					   structure or 0 if no name present. */
2672 	le32 provider_name_offset;	/* Offset in bytes to the name of the
2673 					   cryptographic provider from start of
2674 					   this structure or 0 if no name
2675 					   present. */
2676 /* 16*/	le32 user_name_offset;		/* Offset in bytes to the user name
2677 					   from start of this structure or 0 if
2678 					   no user name present.  (This is also
2679 					   known as lpDisplayInformation.) */
2680 } __attribute__((__packed__)) EFS_DF_CERTIFICATE_THUMBPRINT_HEADER;
2681 
2682 typedef EFS_DF_CERTIFICATE_THUMBPRINT_HEADER EFS_DF_CERT_THUMBPRINT_HEADER;
2683 
2684 typedef enum {
2685 	INTX_SYMBOLIC_LINK =
2686 		const_cpu_to_le64(0x014B4E4C78746E49ULL), /* "IntxLNK\1" */
2687 	INTX_CHARACTER_DEVICE =
2688 		const_cpu_to_le64(0x0052484378746E49ULL), /* "IntxCHR\0" */
2689 	INTX_BLOCK_DEVICE =
2690 		const_cpu_to_le64(0x004B4C4278746E49ULL), /* "IntxBLK\0" */
2691 } INTX_FILE_TYPES;
2692 
2693 typedef struct {
2694 	INTX_FILE_TYPES magic;		/* Intx file magic. */
2695 	union {
2696 		/* For character and block devices. */
2697 		struct {
2698 			le64 major;		/* Major device number. */
2699 			le64 minor;		/* Minor device number. */
2700 			void *device_end[0];	/* Marker for offsetof(). */
2701 		} __attribute__((__packed__));
2702 		/* For symbolic links. */
2703 		ntfschar target[0];
2704 	} __attribute__((__packed__));
2705 } __attribute__((__packed__)) INTX_FILE;
2706 
2707 #endif /* defined _NTFS_LAYOUT_H */
2708