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