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