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

Archive Download this file



interactive