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