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