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