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