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