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