14534a70bSKonstantin Komarov /* SPDX-License-Identifier: GPL-2.0 */
24534a70bSKonstantin Komarov /*
34534a70bSKonstantin Komarov *
44534a70bSKonstantin Komarov * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
54534a70bSKonstantin Komarov *
64534a70bSKonstantin Komarov * on-disk ntfs structs
74534a70bSKonstantin Komarov */
84534a70bSKonstantin Komarov
94534a70bSKonstantin Komarov // clang-format off
1087790b65SKari Argillander #ifndef _LINUX_NTFS3_NTFS_H
1187790b65SKari Argillander #define _LINUX_NTFS3_NTFS_H
124534a70bSKonstantin Komarov
134dfe8332SKari Argillander #include <linux/blkdev.h>
144dfe8332SKari Argillander #include <linux/build_bug.h>
154dfe8332SKari Argillander #include <linux/kernel.h>
164dfe8332SKari Argillander #include <linux/stddef.h>
174dfe8332SKari Argillander #include <linux/string.h>
184dfe8332SKari Argillander #include <linux/types.h>
194dfe8332SKari Argillander
204dfe8332SKari Argillander #include "debug.h"
214dfe8332SKari Argillander
22e8b8e97fSKari Argillander /* TODO: Check 4K MFT record and 512 bytes cluster. */
234534a70bSKonstantin Komarov
24e8b8e97fSKari Argillander /* Check each run for marked clusters. */
254534a70bSKonstantin Komarov #define NTFS3_CHECK_FREE_CLST
264534a70bSKonstantin Komarov
274534a70bSKonstantin Komarov #define NTFS_NAME_LEN 255
284534a70bSKonstantin Komarov
2956eaeb10SKonstantin Komarov /*
3056eaeb10SKonstantin Komarov * ntfs.sys used 500 maximum links on-disk struct allows up to 0xffff.
3156eaeb10SKonstantin Komarov * xfstest generic/041 creates 3003 hardlinks.
3256eaeb10SKonstantin Komarov */
3356eaeb10SKonstantin Komarov #define NTFS_LINK_MAX 4000
344534a70bSKonstantin Komarov
354534a70bSKonstantin Komarov /*
36e8b8e97fSKari Argillander * Activate to use 64 bit clusters instead of 32 bits in ntfs.sys.
37e8b8e97fSKari Argillander * Logical and virtual cluster number if needed, may be
38e8b8e97fSKari Argillander * redefined to use 64 bit value.
394534a70bSKonstantin Komarov */
404534a70bSKonstantin Komarov //#define CONFIG_NTFS3_64BIT_CLUSTER
414534a70bSKonstantin Komarov
424534a70bSKonstantin Komarov #define NTFS_LZNT_MAX_CLUSTER 4096
434534a70bSKonstantin Komarov #define NTFS_LZNT_CUNIT 4
444534a70bSKonstantin Komarov #define NTFS_LZNT_CLUSTERS (1u<<NTFS_LZNT_CUNIT)
454534a70bSKonstantin Komarov
464534a70bSKonstantin Komarov struct GUID {
474534a70bSKonstantin Komarov __le32 Data1;
484534a70bSKonstantin Komarov __le16 Data2;
494534a70bSKonstantin Komarov __le16 Data3;
504534a70bSKonstantin Komarov u8 Data4[8];
514534a70bSKonstantin Komarov };
524534a70bSKonstantin Komarov
534534a70bSKonstantin Komarov /*
54e8b8e97fSKari Argillander * This struct repeats layout of ATTR_FILE_NAME
55e8b8e97fSKari Argillander * at offset 0x40.
56e8b8e97fSKari Argillander * It used to store global constants NAME_MFT/NAME_MIRROR...
57e8b8e97fSKari Argillander * most constant names are shorter than 10.
584534a70bSKonstantin Komarov */
594534a70bSKonstantin Komarov struct cpu_str {
604534a70bSKonstantin Komarov u8 len;
614534a70bSKonstantin Komarov u8 unused;
621fe1c9dcSKonstantin Komarov u16 name[];
634534a70bSKonstantin Komarov };
644534a70bSKonstantin Komarov
654534a70bSKonstantin Komarov struct le_str {
664534a70bSKonstantin Komarov u8 len;
674534a70bSKonstantin Komarov u8 unused;
684534a70bSKonstantin Komarov __le16 name[];
694534a70bSKonstantin Komarov };
704534a70bSKonstantin Komarov
714534a70bSKonstantin Komarov static_assert(SECTOR_SHIFT == 9);
724534a70bSKonstantin Komarov
734534a70bSKonstantin Komarov #ifdef CONFIG_NTFS3_64BIT_CLUSTER
744534a70bSKonstantin Komarov typedef u64 CLST;
754534a70bSKonstantin Komarov static_assert(sizeof(size_t) == 8);
764534a70bSKonstantin Komarov #else
774534a70bSKonstantin Komarov typedef u32 CLST;
784534a70bSKonstantin Komarov #endif
794534a70bSKonstantin Komarov
804534a70bSKonstantin Komarov #define SPARSE_LCN64 ((u64)-1)
814534a70bSKonstantin Komarov #define SPARSE_LCN ((CLST)-1)
824534a70bSKonstantin Komarov #define RESIDENT_LCN ((CLST)-2)
834534a70bSKonstantin Komarov #define COMPRESSED_LCN ((CLST)-3)
844534a70bSKonstantin Komarov
854534a70bSKonstantin Komarov enum RECORD_NUM {
864534a70bSKonstantin Komarov MFT_REC_MFT = 0,
874534a70bSKonstantin Komarov MFT_REC_MIRR = 1,
884534a70bSKonstantin Komarov MFT_REC_LOG = 2,
894534a70bSKonstantin Komarov MFT_REC_VOL = 3,
904534a70bSKonstantin Komarov MFT_REC_ATTR = 4,
914534a70bSKonstantin Komarov MFT_REC_ROOT = 5,
924534a70bSKonstantin Komarov MFT_REC_BITMAP = 6,
934534a70bSKonstantin Komarov MFT_REC_BOOT = 7,
944534a70bSKonstantin Komarov MFT_REC_BADCLUST = 8,
95a81f47c4SKonstantin Komarov MFT_REC_SECURE = 9,
964534a70bSKonstantin Komarov MFT_REC_UPCASE = 10,
97a81f47c4SKonstantin Komarov MFT_REC_EXTEND = 11,
98a81f47c4SKonstantin Komarov MFT_REC_RESERVED = 12,
994534a70bSKonstantin Komarov MFT_REC_FREE = 16,
1004534a70bSKonstantin Komarov MFT_REC_USER = 24,
1014534a70bSKonstantin Komarov };
1024534a70bSKonstantin Komarov
1034534a70bSKonstantin Komarov enum ATTR_TYPE {
1044534a70bSKonstantin Komarov ATTR_ZERO = cpu_to_le32(0x00),
1054534a70bSKonstantin Komarov ATTR_STD = cpu_to_le32(0x10),
1064534a70bSKonstantin Komarov ATTR_LIST = cpu_to_le32(0x20),
1074534a70bSKonstantin Komarov ATTR_NAME = cpu_to_le32(0x30),
1084534a70bSKonstantin Komarov ATTR_ID = cpu_to_le32(0x40),
1094534a70bSKonstantin Komarov ATTR_SECURE = cpu_to_le32(0x50),
1104534a70bSKonstantin Komarov ATTR_LABEL = cpu_to_le32(0x60),
1114534a70bSKonstantin Komarov ATTR_VOL_INFO = cpu_to_le32(0x70),
1124534a70bSKonstantin Komarov ATTR_DATA = cpu_to_le32(0x80),
1134534a70bSKonstantin Komarov ATTR_ROOT = cpu_to_le32(0x90),
1144534a70bSKonstantin Komarov ATTR_ALLOC = cpu_to_le32(0xA0),
1154534a70bSKonstantin Komarov ATTR_BITMAP = cpu_to_le32(0xB0),
1164534a70bSKonstantin Komarov ATTR_REPARSE = cpu_to_le32(0xC0),
1174534a70bSKonstantin Komarov ATTR_EA_INFO = cpu_to_le32(0xD0),
1184534a70bSKonstantin Komarov ATTR_EA = cpu_to_le32(0xE0),
1194534a70bSKonstantin Komarov ATTR_PROPERTYSET = cpu_to_le32(0xF0),
1204534a70bSKonstantin Komarov ATTR_LOGGED_UTILITY_STREAM = cpu_to_le32(0x100),
1214534a70bSKonstantin Komarov ATTR_END = cpu_to_le32(0xFFFFFFFF)
1224534a70bSKonstantin Komarov };
1234534a70bSKonstantin Komarov
1244534a70bSKonstantin Komarov static_assert(sizeof(enum ATTR_TYPE) == 4);
1254534a70bSKonstantin Komarov
1264534a70bSKonstantin Komarov enum FILE_ATTRIBUTE {
1274534a70bSKonstantin Komarov FILE_ATTRIBUTE_READONLY = cpu_to_le32(0x00000001),
1284534a70bSKonstantin Komarov FILE_ATTRIBUTE_HIDDEN = cpu_to_le32(0x00000002),
1294534a70bSKonstantin Komarov FILE_ATTRIBUTE_SYSTEM = cpu_to_le32(0x00000004),
1304534a70bSKonstantin Komarov FILE_ATTRIBUTE_ARCHIVE = cpu_to_le32(0x00000020),
1314534a70bSKonstantin Komarov FILE_ATTRIBUTE_DEVICE = cpu_to_le32(0x00000040),
1324534a70bSKonstantin Komarov FILE_ATTRIBUTE_TEMPORARY = cpu_to_le32(0x00000100),
1334534a70bSKonstantin Komarov FILE_ATTRIBUTE_SPARSE_FILE = cpu_to_le32(0x00000200),
1344534a70bSKonstantin Komarov FILE_ATTRIBUTE_REPARSE_POINT = cpu_to_le32(0x00000400),
1354534a70bSKonstantin Komarov FILE_ATTRIBUTE_COMPRESSED = cpu_to_le32(0x00000800),
1364534a70bSKonstantin Komarov FILE_ATTRIBUTE_OFFLINE = cpu_to_le32(0x00001000),
1374534a70bSKonstantin Komarov FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = cpu_to_le32(0x00002000),
1384534a70bSKonstantin Komarov FILE_ATTRIBUTE_ENCRYPTED = cpu_to_le32(0x00004000),
1394534a70bSKonstantin Komarov FILE_ATTRIBUTE_VALID_FLAGS = cpu_to_le32(0x00007fb7),
1404534a70bSKonstantin Komarov FILE_ATTRIBUTE_DIRECTORY = cpu_to_le32(0x10000000),
141a81f47c4SKonstantin Komarov FILE_ATTRIBUTE_INDEX = cpu_to_le32(0x20000000)
1424534a70bSKonstantin Komarov };
1434534a70bSKonstantin Komarov
1444534a70bSKonstantin Komarov static_assert(sizeof(enum FILE_ATTRIBUTE) == 4);
1454534a70bSKonstantin Komarov
1464534a70bSKonstantin Komarov extern const struct cpu_str NAME_MFT;
1474534a70bSKonstantin Komarov extern const struct cpu_str NAME_MIRROR;
1484534a70bSKonstantin Komarov extern const struct cpu_str NAME_LOGFILE;
1494534a70bSKonstantin Komarov extern const struct cpu_str NAME_VOLUME;
1504534a70bSKonstantin Komarov extern const struct cpu_str NAME_ATTRDEF;
1514534a70bSKonstantin Komarov extern const struct cpu_str NAME_ROOT;
1524534a70bSKonstantin Komarov extern const struct cpu_str NAME_BITMAP;
1534534a70bSKonstantin Komarov extern const struct cpu_str NAME_BOOT;
1544534a70bSKonstantin Komarov extern const struct cpu_str NAME_BADCLUS;
1554534a70bSKonstantin Komarov extern const struct cpu_str NAME_QUOTA;
1564534a70bSKonstantin Komarov extern const struct cpu_str NAME_SECURE;
1574534a70bSKonstantin Komarov extern const struct cpu_str NAME_UPCASE;
1584534a70bSKonstantin Komarov extern const struct cpu_str NAME_EXTEND;
1594534a70bSKonstantin Komarov extern const struct cpu_str NAME_OBJID;
1604534a70bSKonstantin Komarov extern const struct cpu_str NAME_REPARSE;
1614534a70bSKonstantin Komarov extern const struct cpu_str NAME_USNJRNL;
1624534a70bSKonstantin Komarov
1634534a70bSKonstantin Komarov extern const __le16 I30_NAME[4];
1644534a70bSKonstantin Komarov extern const __le16 SII_NAME[4];
1654534a70bSKonstantin Komarov extern const __le16 SDH_NAME[4];
1664534a70bSKonstantin Komarov extern const __le16 SO_NAME[2];
1674534a70bSKonstantin Komarov extern const __le16 SQ_NAME[2];
1684534a70bSKonstantin Komarov extern const __le16 SR_NAME[2];
1694534a70bSKonstantin Komarov
1704534a70bSKonstantin Komarov extern const __le16 BAD_NAME[4];
1714534a70bSKonstantin Komarov extern const __le16 SDS_NAME[4];
1724534a70bSKonstantin Komarov extern const __le16 WOF_NAME[17]; /* WofCompressedData */
1734534a70bSKonstantin Komarov
174e8b8e97fSKari Argillander /* MFT record number structure. */
1754534a70bSKonstantin Komarov struct MFT_REF {
176e8b8e97fSKari Argillander __le32 low; // The low part of the number.
177e8b8e97fSKari Argillander __le16 high; // The high part of the number.
178e8b8e97fSKari Argillander __le16 seq; // The sequence number of MFT record.
1794534a70bSKonstantin Komarov };
1804534a70bSKonstantin Komarov
1814534a70bSKonstantin Komarov static_assert(sizeof(__le64) == sizeof(struct MFT_REF));
1824534a70bSKonstantin Komarov
ino_get(const struct MFT_REF * ref)1834534a70bSKonstantin Komarov static inline CLST ino_get(const struct MFT_REF *ref)
1844534a70bSKonstantin Komarov {
1854534a70bSKonstantin Komarov #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1864534a70bSKonstantin Komarov return le32_to_cpu(ref->low) | ((u64)le16_to_cpu(ref->high) << 32);
1874534a70bSKonstantin Komarov #else
1884534a70bSKonstantin Komarov return le32_to_cpu(ref->low);
1894534a70bSKonstantin Komarov #endif
1904534a70bSKonstantin Komarov }
1914534a70bSKonstantin Komarov
1924534a70bSKonstantin Komarov struct NTFS_BOOT {
193e8b8e97fSKari Argillander u8 jump_code[3]; // 0x00: Jump to boot code.
1944534a70bSKonstantin Komarov u8 system_id[8]; // 0x03: System ID, equals "NTFS "
1954534a70bSKonstantin Komarov
196e8b8e97fSKari Argillander // NOTE: This member is not aligned(!)
197e8b8e97fSKari Argillander // bytes_per_sector[0] must be 0.
198e8b8e97fSKari Argillander // bytes_per_sector[1] must be multiplied by 256.
199e8b8e97fSKari Argillander u8 bytes_per_sector[2]; // 0x0B: Bytes per sector.
2004534a70bSKonstantin Komarov
201e8b8e97fSKari Argillander u8 sectors_per_clusters;// 0x0D: Sectors per cluster.
2024534a70bSKonstantin Komarov u8 unused1[7];
2034534a70bSKonstantin Komarov u8 media_type; // 0x15: Media type (0xF8 - harddisk)
2044534a70bSKonstantin Komarov u8 unused2[2];
205e8b8e97fSKari Argillander __le16 sct_per_track; // 0x18: number of sectors per track.
206e8b8e97fSKari Argillander __le16 heads; // 0x1A: number of heads per cylinder.
207e8b8e97fSKari Argillander __le32 hidden_sectors; // 0x1C: number of 'hidden' sectors.
2084534a70bSKonstantin Komarov u8 unused3[4];
209e8b8e97fSKari Argillander u8 bios_drive_num; // 0x24: BIOS drive number =0x80.
2104534a70bSKonstantin Komarov u8 unused4;
211e8b8e97fSKari Argillander u8 signature_ex; // 0x26: Extended BOOT signature =0x80.
2124534a70bSKonstantin Komarov u8 unused5;
213e8b8e97fSKari Argillander __le64 sectors_per_volume;// 0x28: Size of volume in sectors.
214e8b8e97fSKari Argillander __le64 mft_clst; // 0x30: First cluster of $MFT
215e8b8e97fSKari Argillander __le64 mft2_clst; // 0x38: First cluster of $MFTMirr
216e8b8e97fSKari Argillander s8 record_size; // 0x40: Size of MFT record in clusters(sectors).
2174534a70bSKonstantin Komarov u8 unused6[3];
218e8b8e97fSKari Argillander s8 index_size; // 0x44: Size of INDX record in clusters(sectors).
2194534a70bSKonstantin Komarov u8 unused7[3];
2204534a70bSKonstantin Komarov __le64 serial_num; // 0x48: Volume serial number
2214534a70bSKonstantin Komarov __le32 check_sum; // 0x50: Simple additive checksum of all
222e8b8e97fSKari Argillander // of the u32's which precede the 'check_sum'.
2234534a70bSKonstantin Komarov
2244534a70bSKonstantin Komarov u8 boot_code[0x200 - 0x50 - 2 - 4]; // 0x54:
2254534a70bSKonstantin Komarov u8 boot_magic[2]; // 0x1FE: Boot signature =0x55 + 0xAA
2264534a70bSKonstantin Komarov };
2274534a70bSKonstantin Komarov
2284534a70bSKonstantin Komarov static_assert(sizeof(struct NTFS_BOOT) == 0x200);
2294534a70bSKonstantin Komarov
2304534a70bSKonstantin Komarov enum NTFS_SIGNATURE {
2314534a70bSKonstantin Komarov NTFS_FILE_SIGNATURE = cpu_to_le32(0x454C4946), // 'FILE'
2324534a70bSKonstantin Komarov NTFS_INDX_SIGNATURE = cpu_to_le32(0x58444E49), // 'INDX'
2334534a70bSKonstantin Komarov NTFS_CHKD_SIGNATURE = cpu_to_le32(0x444B4843), // 'CHKD'
2344534a70bSKonstantin Komarov NTFS_RSTR_SIGNATURE = cpu_to_le32(0x52545352), // 'RSTR'
2354534a70bSKonstantin Komarov NTFS_RCRD_SIGNATURE = cpu_to_le32(0x44524352), // 'RCRD'
2364534a70bSKonstantin Komarov NTFS_BAAD_SIGNATURE = cpu_to_le32(0x44414142), // 'BAAD'
2374534a70bSKonstantin Komarov NTFS_HOLE_SIGNATURE = cpu_to_le32(0x454C4F48), // 'HOLE'
2384534a70bSKonstantin Komarov NTFS_FFFF_SIGNATURE = cpu_to_le32(0xffffffff),
2394534a70bSKonstantin Komarov };
2404534a70bSKonstantin Komarov
2414534a70bSKonstantin Komarov static_assert(sizeof(enum NTFS_SIGNATURE) == 4);
2424534a70bSKonstantin Komarov
243e8b8e97fSKari Argillander /* MFT Record header structure. */
2444534a70bSKonstantin Komarov struct NTFS_RECORD_HEADER {
245e8b8e97fSKari Argillander /* Record magic number, equals 'FILE'/'INDX'/'RSTR'/'RCRD'. */
2464534a70bSKonstantin Komarov enum NTFS_SIGNATURE sign; // 0x00:
2474534a70bSKonstantin Komarov __le16 fix_off; // 0x04:
2484534a70bSKonstantin Komarov __le16 fix_num; // 0x06:
249e8b8e97fSKari Argillander __le64 lsn; // 0x08: Log file sequence number,
2504534a70bSKonstantin Komarov };
2514534a70bSKonstantin Komarov
2524534a70bSKonstantin Komarov static_assert(sizeof(struct NTFS_RECORD_HEADER) == 0x10);
2534534a70bSKonstantin Komarov
is_baad(const struct NTFS_RECORD_HEADER * hdr)2544534a70bSKonstantin Komarov static inline int is_baad(const struct NTFS_RECORD_HEADER *hdr)
2554534a70bSKonstantin Komarov {
2564534a70bSKonstantin Komarov return hdr->sign == NTFS_BAAD_SIGNATURE;
2574534a70bSKonstantin Komarov }
2584534a70bSKonstantin Komarov
259e8b8e97fSKari Argillander /* Possible bits in struct MFT_REC.flags. */
2604534a70bSKonstantin Komarov enum RECORD_FLAG {
2614534a70bSKonstantin Komarov RECORD_FLAG_IN_USE = cpu_to_le16(0x0001),
2624534a70bSKonstantin Komarov RECORD_FLAG_DIR = cpu_to_le16(0x0002),
2634534a70bSKonstantin Komarov RECORD_FLAG_SYSTEM = cpu_to_le16(0x0004),
264a81f47c4SKonstantin Komarov RECORD_FLAG_INDEX = cpu_to_le16(0x0008),
2654534a70bSKonstantin Komarov };
2664534a70bSKonstantin Komarov
267d3624466SKonstantin Komarov /* MFT Record structure. */
2684534a70bSKonstantin Komarov struct MFT_REC {
2694534a70bSKonstantin Komarov struct NTFS_RECORD_HEADER rhdr; // 'FILE'
2704534a70bSKonstantin Komarov
271e8b8e97fSKari Argillander __le16 seq; // 0x10: Sequence number for this record.
272e8b8e97fSKari Argillander __le16 hard_links; // 0x12: The number of hard links to record.
273e8b8e97fSKari Argillander __le16 attr_off; // 0x14: Offset to attributes.
274e8b8e97fSKari Argillander __le16 flags; // 0x16: See RECORD_FLAG.
275e8b8e97fSKari Argillander __le32 used; // 0x18: The size of used part.
276e8b8e97fSKari Argillander __le32 total; // 0x1C: Total record size.
2774534a70bSKonstantin Komarov
278e8b8e97fSKari Argillander struct MFT_REF parent_ref; // 0x20: Parent MFT record.
279e8b8e97fSKari Argillander __le16 next_attr_id; // 0x28: The next attribute Id.
2804534a70bSKonstantin Komarov
281e8b8e97fSKari Argillander __le16 res; // 0x2A: High part of MFT record?
282e8b8e97fSKari Argillander __le32 mft_record; // 0x2C: Current MFT record number.
2834534a70bSKonstantin Komarov __le16 fixups[]; // 0x30:
2844534a70bSKonstantin Komarov };
2854534a70bSKonstantin Komarov
2864534a70bSKonstantin Komarov #define MFTRECORD_FIXUP_OFFSET_1 offsetof(struct MFT_REC, res)
2874534a70bSKonstantin Komarov #define MFTRECORD_FIXUP_OFFSET_3 offsetof(struct MFT_REC, fixups)
28833e70701SKonstantin Komarov /*
28933e70701SKonstantin Komarov * define MFTRECORD_FIXUP_OFFSET as MFTRECORD_FIXUP_OFFSET_3 (0x30)
29033e70701SKonstantin Komarov * to format new mft records with bigger header (as current ntfs.sys does)
29133e70701SKonstantin Komarov *
29233e70701SKonstantin Komarov * define MFTRECORD_FIXUP_OFFSET as MFTRECORD_FIXUP_OFFSET_1 (0x2A)
29333e70701SKonstantin Komarov * to format new mft records with smaller header (as old ntfs.sys did)
29433e70701SKonstantin Komarov * Both variants are valid.
29533e70701SKonstantin Komarov */
29633e70701SKonstantin Komarov #define MFTRECORD_FIXUP_OFFSET MFTRECORD_FIXUP_OFFSET_1
2974534a70bSKonstantin Komarov
2984534a70bSKonstantin Komarov static_assert(MFTRECORD_FIXUP_OFFSET_1 == 0x2A);
2994534a70bSKonstantin Komarov static_assert(MFTRECORD_FIXUP_OFFSET_3 == 0x30);
3004534a70bSKonstantin Komarov
is_rec_base(const struct MFT_REC * rec)3014534a70bSKonstantin Komarov static inline bool is_rec_base(const struct MFT_REC *rec)
3024534a70bSKonstantin Komarov {
3034534a70bSKonstantin Komarov const struct MFT_REF *r = &rec->parent_ref;
3044534a70bSKonstantin Komarov
3054534a70bSKonstantin Komarov return !r->low && !r->high && !r->seq;
3064534a70bSKonstantin Komarov }
3074534a70bSKonstantin Komarov
is_mft_rec5(const struct MFT_REC * rec)3084534a70bSKonstantin Komarov static inline bool is_mft_rec5(const struct MFT_REC *rec)
3094534a70bSKonstantin Komarov {
3104534a70bSKonstantin Komarov return le16_to_cpu(rec->rhdr.fix_off) >=
3114534a70bSKonstantin Komarov offsetof(struct MFT_REC, fixups);
3124534a70bSKonstantin Komarov }
3134534a70bSKonstantin Komarov
is_rec_inuse(const struct MFT_REC * rec)3144534a70bSKonstantin Komarov static inline bool is_rec_inuse(const struct MFT_REC *rec)
3154534a70bSKonstantin Komarov {
3164534a70bSKonstantin Komarov return rec->flags & RECORD_FLAG_IN_USE;
3174534a70bSKonstantin Komarov }
3184534a70bSKonstantin Komarov
clear_rec_inuse(struct MFT_REC * rec)3194534a70bSKonstantin Komarov static inline bool clear_rec_inuse(struct MFT_REC *rec)
3204534a70bSKonstantin Komarov {
3214534a70bSKonstantin Komarov return rec->flags &= ~RECORD_FLAG_IN_USE;
3224534a70bSKonstantin Komarov }
3234534a70bSKonstantin Komarov
3244534a70bSKonstantin Komarov /* Possible values of ATTR_RESIDENT.flags */
3254534a70bSKonstantin Komarov #define RESIDENT_FLAG_INDEXED 0x01
3264534a70bSKonstantin Komarov
3274534a70bSKonstantin Komarov struct ATTR_RESIDENT {
328e8b8e97fSKari Argillander __le32 data_size; // 0x10: The size of data.
329e8b8e97fSKari Argillander __le16 data_off; // 0x14: Offset to data.
330e8b8e97fSKari Argillander u8 flags; // 0x16: Resident flags ( 1 - indexed ).
3314534a70bSKonstantin Komarov u8 res; // 0x17:
3324534a70bSKonstantin Komarov }; // sizeof() = 0x18
3334534a70bSKonstantin Komarov
3344534a70bSKonstantin Komarov struct ATTR_NONRESIDENT {
335e8b8e97fSKari Argillander __le64 svcn; // 0x10: Starting VCN of this segment.
336e8b8e97fSKari Argillander __le64 evcn; // 0x18: End VCN of this segment.
337e8b8e97fSKari Argillander __le16 run_off; // 0x20: Offset to packed runs.
3384534a70bSKonstantin Komarov // Unit of Compression size for this stream, expressed
3394534a70bSKonstantin Komarov // as a log of the cluster size.
3404534a70bSKonstantin Komarov //
3414534a70bSKonstantin Komarov // 0 means file is not compressed
3424534a70bSKonstantin Komarov // 1, 2, 3, and 4 are potentially legal values if the
3434534a70bSKonstantin Komarov // stream is compressed, however the implementation
344a81f47c4SKonstantin Komarov // may only choose to use 4, or possibly 3.
345a81f47c4SKonstantin Komarov // Note that 4 means cluster size time 16.
346a81f47c4SKonstantin Komarov // If convenient the implementation may wish to accept a
3474534a70bSKonstantin Komarov // reasonable range of legal values here (1-5?),
3484534a70bSKonstantin Komarov // even if the implementation only generates
3494534a70bSKonstantin Komarov // a smaller set of values itself.
350e8b8e97fSKari Argillander u8 c_unit; // 0x22:
3514534a70bSKonstantin Komarov u8 res1[5]; // 0x23:
352e8b8e97fSKari Argillander __le64 alloc_size; // 0x28: The allocated size of attribute in bytes.
3534534a70bSKonstantin Komarov // (multiple of cluster size)
354e8b8e97fSKari Argillander __le64 data_size; // 0x30: The size of attribute in bytes <= alloc_size.
355e8b8e97fSKari Argillander __le64 valid_size; // 0x38: The size of valid part in bytes <= data_size.
356e8b8e97fSKari Argillander __le64 total_size; // 0x40: The sum of the allocated clusters for a file.
3574534a70bSKonstantin Komarov // (present only for the first segment (0 == vcn)
3584534a70bSKonstantin Komarov // of compressed attribute)
3594534a70bSKonstantin Komarov
3604534a70bSKonstantin Komarov }; // sizeof()=0x40 or 0x48 (if compressed)
3614534a70bSKonstantin Komarov
3624534a70bSKonstantin Komarov /* Possible values of ATTRIB.flags: */
3634534a70bSKonstantin Komarov #define ATTR_FLAG_COMPRESSED cpu_to_le16(0x0001)
3644534a70bSKonstantin Komarov #define ATTR_FLAG_COMPRESSED_MASK cpu_to_le16(0x00FF)
3654534a70bSKonstantin Komarov #define ATTR_FLAG_ENCRYPTED cpu_to_le16(0x4000)
3664534a70bSKonstantin Komarov #define ATTR_FLAG_SPARSED cpu_to_le16(0x8000)
3674534a70bSKonstantin Komarov
3684534a70bSKonstantin Komarov struct ATTRIB {
369e8b8e97fSKari Argillander enum ATTR_TYPE type; // 0x00: The type of this attribute.
370e8b8e97fSKari Argillander __le32 size; // 0x04: The size of this attribute.
3714534a70bSKonstantin Komarov u8 non_res; // 0x08: Is this attribute non-resident?
372e8b8e97fSKari Argillander u8 name_len; // 0x09: This attribute name length.
373e8b8e97fSKari Argillander __le16 name_off; // 0x0A: Offset to the attribute name.
374e8b8e97fSKari Argillander __le16 flags; // 0x0C: See ATTR_FLAG_XXX.
375e8b8e97fSKari Argillander __le16 id; // 0x0E: Unique id (per record).
3764534a70bSKonstantin Komarov
3774534a70bSKonstantin Komarov union {
3784534a70bSKonstantin Komarov struct ATTR_RESIDENT res; // 0x10
3794534a70bSKonstantin Komarov struct ATTR_NONRESIDENT nres; // 0x10
3804534a70bSKonstantin Komarov };
3814534a70bSKonstantin Komarov };
3824534a70bSKonstantin Komarov
383e8b8e97fSKari Argillander /* Define attribute sizes. */
3844534a70bSKonstantin Komarov #define SIZEOF_RESIDENT 0x18
3854534a70bSKonstantin Komarov #define SIZEOF_NONRESIDENT_EX 0x48
3864534a70bSKonstantin Komarov #define SIZEOF_NONRESIDENT 0x40
3874534a70bSKonstantin Komarov
3884534a70bSKonstantin Komarov #define SIZEOF_RESIDENT_LE cpu_to_le16(0x18)
3894534a70bSKonstantin Komarov #define SIZEOF_NONRESIDENT_EX_LE cpu_to_le16(0x48)
3904534a70bSKonstantin Komarov #define SIZEOF_NONRESIDENT_LE cpu_to_le16(0x40)
3914534a70bSKonstantin Komarov
attr_ondisk_size(const struct ATTRIB * attr)3924534a70bSKonstantin Komarov static inline u64 attr_ondisk_size(const struct ATTRIB *attr)
3934534a70bSKonstantin Komarov {
3944534a70bSKonstantin Komarov return attr->non_res ? ((attr->flags &
3954534a70bSKonstantin Komarov (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ?
3964534a70bSKonstantin Komarov le64_to_cpu(attr->nres.total_size) :
397fa3cacf5SKari Argillander le64_to_cpu(attr->nres.alloc_size))
398fa3cacf5SKari Argillander : ALIGN(le32_to_cpu(attr->res.data_size), 8);
3994534a70bSKonstantin Komarov }
4004534a70bSKonstantin Komarov
attr_size(const struct ATTRIB * attr)4014534a70bSKonstantin Komarov static inline u64 attr_size(const struct ATTRIB *attr)
4024534a70bSKonstantin Komarov {
4034534a70bSKonstantin Komarov return attr->non_res ? le64_to_cpu(attr->nres.data_size) :
4044534a70bSKonstantin Komarov le32_to_cpu(attr->res.data_size);
4054534a70bSKonstantin Komarov }
4064534a70bSKonstantin Komarov
is_attr_encrypted(const struct ATTRIB * attr)4074534a70bSKonstantin Komarov static inline bool is_attr_encrypted(const struct ATTRIB *attr)
4084534a70bSKonstantin Komarov {
4094534a70bSKonstantin Komarov return attr->flags & ATTR_FLAG_ENCRYPTED;
4104534a70bSKonstantin Komarov }
4114534a70bSKonstantin Komarov
is_attr_sparsed(const struct ATTRIB * attr)4124534a70bSKonstantin Komarov static inline bool is_attr_sparsed(const struct ATTRIB *attr)
4134534a70bSKonstantin Komarov {
4144534a70bSKonstantin Komarov return attr->flags & ATTR_FLAG_SPARSED;
4154534a70bSKonstantin Komarov }
4164534a70bSKonstantin Komarov
is_attr_compressed(const struct ATTRIB * attr)4174534a70bSKonstantin Komarov static inline bool is_attr_compressed(const struct ATTRIB *attr)
4184534a70bSKonstantin Komarov {
4194534a70bSKonstantin Komarov return attr->flags & ATTR_FLAG_COMPRESSED;
4204534a70bSKonstantin Komarov }
4214534a70bSKonstantin Komarov
is_attr_ext(const struct ATTRIB * attr)4224534a70bSKonstantin Komarov static inline bool is_attr_ext(const struct ATTRIB *attr)
4234534a70bSKonstantin Komarov {
4244534a70bSKonstantin Komarov return attr->flags & (ATTR_FLAG_SPARSED | ATTR_FLAG_COMPRESSED);
4254534a70bSKonstantin Komarov }
4264534a70bSKonstantin Komarov
is_attr_indexed(const struct ATTRIB * attr)4274534a70bSKonstantin Komarov static inline bool is_attr_indexed(const struct ATTRIB *attr)
4284534a70bSKonstantin Komarov {
4294534a70bSKonstantin Komarov return !attr->non_res && (attr->res.flags & RESIDENT_FLAG_INDEXED);
4304534a70bSKonstantin Komarov }
4314534a70bSKonstantin Komarov
attr_name(const struct ATTRIB * attr)4324534a70bSKonstantin Komarov static inline __le16 const *attr_name(const struct ATTRIB *attr)
4334534a70bSKonstantin Komarov {
4344534a70bSKonstantin Komarov return Add2Ptr(attr, le16_to_cpu(attr->name_off));
4354534a70bSKonstantin Komarov }
4364534a70bSKonstantin Komarov
attr_svcn(const struct ATTRIB * attr)4374534a70bSKonstantin Komarov static inline u64 attr_svcn(const struct ATTRIB *attr)
4384534a70bSKonstantin Komarov {
4394534a70bSKonstantin Komarov return attr->non_res ? le64_to_cpu(attr->nres.svcn) : 0;
4404534a70bSKonstantin Komarov }
4414534a70bSKonstantin Komarov
4424534a70bSKonstantin Komarov static_assert(sizeof(struct ATTRIB) == 0x48);
4434534a70bSKonstantin Komarov static_assert(sizeof(((struct ATTRIB *)NULL)->res) == 0x08);
4444534a70bSKonstantin Komarov static_assert(sizeof(((struct ATTRIB *)NULL)->nres) == 0x38);
4454534a70bSKonstantin Komarov
resident_data_ex(const struct ATTRIB * attr,u32 datasize)4464534a70bSKonstantin Komarov static inline void *resident_data_ex(const struct ATTRIB *attr, u32 datasize)
4474534a70bSKonstantin Komarov {
4484534a70bSKonstantin Komarov u32 asize, rsize;
4494534a70bSKonstantin Komarov u16 off;
4504534a70bSKonstantin Komarov
4514534a70bSKonstantin Komarov if (attr->non_res)
4524534a70bSKonstantin Komarov return NULL;
4534534a70bSKonstantin Komarov
4544534a70bSKonstantin Komarov asize = le32_to_cpu(attr->size);
4554534a70bSKonstantin Komarov off = le16_to_cpu(attr->res.data_off);
4564534a70bSKonstantin Komarov
4574534a70bSKonstantin Komarov if (asize < datasize + off)
4584534a70bSKonstantin Komarov return NULL;
4594534a70bSKonstantin Komarov
4604534a70bSKonstantin Komarov rsize = le32_to_cpu(attr->res.data_size);
4614534a70bSKonstantin Komarov if (rsize < datasize)
4624534a70bSKonstantin Komarov return NULL;
4634534a70bSKonstantin Komarov
4644534a70bSKonstantin Komarov return Add2Ptr(attr, off);
4654534a70bSKonstantin Komarov }
4664534a70bSKonstantin Komarov
resident_data(const struct ATTRIB * attr)4674534a70bSKonstantin Komarov static inline void *resident_data(const struct ATTRIB *attr)
4684534a70bSKonstantin Komarov {
4694534a70bSKonstantin Komarov return Add2Ptr(attr, le16_to_cpu(attr->res.data_off));
4704534a70bSKonstantin Komarov }
4714534a70bSKonstantin Komarov
attr_run(const struct ATTRIB * attr)4724534a70bSKonstantin Komarov static inline void *attr_run(const struct ATTRIB *attr)
4734534a70bSKonstantin Komarov {
4744534a70bSKonstantin Komarov return Add2Ptr(attr, le16_to_cpu(attr->nres.run_off));
4754534a70bSKonstantin Komarov }
4764534a70bSKonstantin Komarov
477e8b8e97fSKari Argillander /* Standard information attribute (0x10). */
4784534a70bSKonstantin Komarov struct ATTR_STD_INFO {
479e8b8e97fSKari Argillander __le64 cr_time; // 0x00: File creation file.
480e8b8e97fSKari Argillander __le64 m_time; // 0x08: File modification time.
481e8b8e97fSKari Argillander __le64 c_time; // 0x10: Last time any attribute was modified.
482e8b8e97fSKari Argillander __le64 a_time; // 0x18: File last access time.
483e8b8e97fSKari Argillander enum FILE_ATTRIBUTE fa; // 0x20: Standard DOS attributes & more.
484e8b8e97fSKari Argillander __le32 max_ver_num; // 0x24: Maximum Number of Versions.
485e8b8e97fSKari Argillander __le32 ver_num; // 0x28: Version Number.
486e8b8e97fSKari Argillander __le32 class_id; // 0x2C: Class Id from bidirectional Class Id index.
4874534a70bSKonstantin Komarov };
4884534a70bSKonstantin Komarov
4894534a70bSKonstantin Komarov static_assert(sizeof(struct ATTR_STD_INFO) == 0x30);
4904534a70bSKonstantin Komarov
4914534a70bSKonstantin Komarov #define SECURITY_ID_INVALID 0x00000000
4924534a70bSKonstantin Komarov #define SECURITY_ID_FIRST 0x00000100
4934534a70bSKonstantin Komarov
4944534a70bSKonstantin Komarov struct ATTR_STD_INFO5 {
495e8b8e97fSKari Argillander __le64 cr_time; // 0x00: File creation file.
496e8b8e97fSKari Argillander __le64 m_time; // 0x08: File modification time.
497e8b8e97fSKari Argillander __le64 c_time; // 0x10: Last time any attribute was modified.
498e8b8e97fSKari Argillander __le64 a_time; // 0x18: File last access time.
499e8b8e97fSKari Argillander enum FILE_ATTRIBUTE fa; // 0x20: Standard DOS attributes & more.
500e8b8e97fSKari Argillander __le32 max_ver_num; // 0x24: Maximum Number of Versions.
501e8b8e97fSKari Argillander __le32 ver_num; // 0x28: Version Number.
502e8b8e97fSKari Argillander __le32 class_id; // 0x2C: Class Id from bidirectional Class Id index.
5034534a70bSKonstantin Komarov
5044534a70bSKonstantin Komarov __le32 owner_id; // 0x30: Owner Id of the user owning the file.
505e8b8e97fSKari Argillander __le32 security_id; // 0x34: The Security Id is a key in the $SII Index and $SDS.
5064534a70bSKonstantin Komarov __le64 quota_charge; // 0x38:
5074534a70bSKonstantin Komarov __le64 usn; // 0x40: Last Update Sequence Number of the file. This is a direct
5084534a70bSKonstantin Komarov // index into the file $UsnJrnl. If zero, the USN Journal is
5094534a70bSKonstantin Komarov // disabled.
5104534a70bSKonstantin Komarov };
5114534a70bSKonstantin Komarov
5124534a70bSKonstantin Komarov static_assert(sizeof(struct ATTR_STD_INFO5) == 0x48);
5134534a70bSKonstantin Komarov
514e8b8e97fSKari Argillander /* Attribute list entry structure (0x20) */
5154534a70bSKonstantin Komarov struct ATTR_LIST_ENTRY {
516e8b8e97fSKari Argillander enum ATTR_TYPE type; // 0x00: The type of attribute.
517e8b8e97fSKari Argillander __le16 size; // 0x04: The size of this record.
518e8b8e97fSKari Argillander u8 name_len; // 0x06: The length of attribute name.
519e8b8e97fSKari Argillander u8 name_off; // 0x07: The offset to attribute name.
520e8b8e97fSKari Argillander __le64 vcn; // 0x08: Starting VCN of this attribute.
521e8b8e97fSKari Argillander struct MFT_REF ref; // 0x10: MFT record number with attribute.
522e8b8e97fSKari Argillander __le16 id; // 0x18: struct ATTRIB ID.
52307b91863SKonstantin Komarov __le16 name[]; // 0x1A: Just to align. To get real name can use name_off.
5244534a70bSKonstantin Komarov
5254534a70bSKonstantin Komarov }; // sizeof(0x20)
5264534a70bSKonstantin Komarov
le_size(u8 name_len)5274534a70bSKonstantin Komarov static inline u32 le_size(u8 name_len)
5284534a70bSKonstantin Komarov {
529fa3cacf5SKari Argillander return ALIGN(offsetof(struct ATTR_LIST_ENTRY, name) +
530fa3cacf5SKari Argillander name_len * sizeof(short), 8);
5314534a70bSKonstantin Komarov }
5324534a70bSKonstantin Komarov
533e8b8e97fSKari Argillander /* Returns 0 if 'attr' has the same type and name. */
le_cmp(const struct ATTR_LIST_ENTRY * le,const struct ATTRIB * attr)5344534a70bSKonstantin Komarov static inline int le_cmp(const struct ATTR_LIST_ENTRY *le,
5354534a70bSKonstantin Komarov const struct ATTRIB *attr)
5364534a70bSKonstantin Komarov {
5374534a70bSKonstantin Komarov return le->type != attr->type || le->name_len != attr->name_len ||
5384534a70bSKonstantin Komarov (!le->name_len &&
5394534a70bSKonstantin Komarov memcmp(Add2Ptr(le, le->name_off),
5404534a70bSKonstantin Komarov Add2Ptr(attr, le16_to_cpu(attr->name_off)),
5414534a70bSKonstantin Komarov le->name_len * sizeof(short)));
5424534a70bSKonstantin Komarov }
5434534a70bSKonstantin Komarov
le_name(const struct ATTR_LIST_ENTRY * le)5444534a70bSKonstantin Komarov static inline __le16 const *le_name(const struct ATTR_LIST_ENTRY *le)
5454534a70bSKonstantin Komarov {
5464534a70bSKonstantin Komarov return Add2Ptr(le, le->name_off);
5474534a70bSKonstantin Komarov }
5484534a70bSKonstantin Komarov
549e8b8e97fSKari Argillander /* File name types (the field type in struct ATTR_FILE_NAME). */
5504534a70bSKonstantin Komarov #define FILE_NAME_POSIX 0
5514534a70bSKonstantin Komarov #define FILE_NAME_UNICODE 1
5524534a70bSKonstantin Komarov #define FILE_NAME_DOS 2
5534534a70bSKonstantin Komarov #define FILE_NAME_UNICODE_AND_DOS (FILE_NAME_DOS | FILE_NAME_UNICODE)
5544534a70bSKonstantin Komarov
555e8b8e97fSKari Argillander /* Filename attribute structure (0x30). */
5564534a70bSKonstantin Komarov struct NTFS_DUP_INFO {
557e8b8e97fSKari Argillander __le64 cr_time; // 0x00: File creation file.
558e8b8e97fSKari Argillander __le64 m_time; // 0x08: File modification time.
559e8b8e97fSKari Argillander __le64 c_time; // 0x10: Last time any attribute was modified.
560e8b8e97fSKari Argillander __le64 a_time; // 0x18: File last access time.
561e8b8e97fSKari Argillander __le64 alloc_size; // 0x20: Data attribute allocated size, multiple of cluster size.
562e8b8e97fSKari Argillander __le64 data_size; // 0x28: Data attribute size <= Dataalloc_size.
563e8b8e97fSKari Argillander enum FILE_ATTRIBUTE fa; // 0x30: Standard DOS attributes & more.
564e8b8e97fSKari Argillander __le16 ea_size; // 0x34: Packed EAs.
565e8b8e97fSKari Argillander __le16 reparse; // 0x36: Used by Reparse.
5664534a70bSKonstantin Komarov
5674534a70bSKonstantin Komarov }; // 0x38
5684534a70bSKonstantin Komarov
5694534a70bSKonstantin Komarov struct ATTR_FILE_NAME {
570e8b8e97fSKari Argillander struct MFT_REF home; // 0x00: MFT record for directory.
571e8b8e97fSKari Argillander struct NTFS_DUP_INFO dup;// 0x08:
572e8b8e97fSKari Argillander u8 name_len; // 0x40: File name length in words.
573e8b8e97fSKari Argillander u8 type; // 0x41: File name type.
574e8b8e97fSKari Argillander __le16 name[]; // 0x42: File name.
5754534a70bSKonstantin Komarov };
5764534a70bSKonstantin Komarov
5774534a70bSKonstantin Komarov static_assert(sizeof(((struct ATTR_FILE_NAME *)NULL)->dup) == 0x38);
5784534a70bSKonstantin Komarov static_assert(offsetof(struct ATTR_FILE_NAME, name) == 0x42);
5794534a70bSKonstantin Komarov #define SIZEOF_ATTRIBUTE_FILENAME 0x44
5804534a70bSKonstantin Komarov #define SIZEOF_ATTRIBUTE_FILENAME_MAX (0x42 + 255 * 2)
5814534a70bSKonstantin Komarov
attr_from_name(struct ATTR_FILE_NAME * fname)5824534a70bSKonstantin Komarov static inline struct ATTRIB *attr_from_name(struct ATTR_FILE_NAME *fname)
5834534a70bSKonstantin Komarov {
5844534a70bSKonstantin Komarov return (struct ATTRIB *)((char *)fname - SIZEOF_RESIDENT);
5854534a70bSKonstantin Komarov }
5864534a70bSKonstantin Komarov
fname_full_size(const struct ATTR_FILE_NAME * fname)5874534a70bSKonstantin Komarov static inline u16 fname_full_size(const struct ATTR_FILE_NAME *fname)
5884534a70bSKonstantin Komarov {
589e8b8e97fSKari Argillander /* Don't return struct_size(fname, name, fname->name_len); */
5904534a70bSKonstantin Komarov return offsetof(struct ATTR_FILE_NAME, name) +
5914534a70bSKonstantin Komarov fname->name_len * sizeof(short);
5924534a70bSKonstantin Komarov }
5934534a70bSKonstantin Komarov
paired_name(u8 type)5944534a70bSKonstantin Komarov static inline u8 paired_name(u8 type)
5954534a70bSKonstantin Komarov {
5964534a70bSKonstantin Komarov if (type == FILE_NAME_UNICODE)
5974534a70bSKonstantin Komarov return FILE_NAME_DOS;
5984534a70bSKonstantin Komarov if (type == FILE_NAME_DOS)
5994534a70bSKonstantin Komarov return FILE_NAME_UNICODE;
6004534a70bSKonstantin Komarov return FILE_NAME_POSIX;
6014534a70bSKonstantin Komarov }
6024534a70bSKonstantin Komarov
603e8b8e97fSKari Argillander /* Index entry defines ( the field flags in NtfsDirEntry ). */
6044534a70bSKonstantin Komarov #define NTFS_IE_HAS_SUBNODES cpu_to_le16(1)
6054534a70bSKonstantin Komarov #define NTFS_IE_LAST cpu_to_le16(2)
6064534a70bSKonstantin Komarov
607e8b8e97fSKari Argillander /* Directory entry structure. */
6084534a70bSKonstantin Komarov struct NTFS_DE {
6094534a70bSKonstantin Komarov union {
610e8b8e97fSKari Argillander struct MFT_REF ref; // 0x00: MFT record number with this file.
6114534a70bSKonstantin Komarov struct {
6124534a70bSKonstantin Komarov __le16 data_off; // 0x00:
6134534a70bSKonstantin Komarov __le16 data_size; // 0x02:
614e8b8e97fSKari Argillander __le32 res; // 0x04: Must be 0.
6154534a70bSKonstantin Komarov } view;
6164534a70bSKonstantin Komarov };
617e8b8e97fSKari Argillander __le16 size; // 0x08: The size of this entry.
618e8b8e97fSKari Argillander __le16 key_size; // 0x0A: The size of File name length in bytes + 0x42.
619e8b8e97fSKari Argillander __le16 flags; // 0x0C: Entry flags: NTFS_IE_XXX.
6204534a70bSKonstantin Komarov __le16 res; // 0x0E:
6214534a70bSKonstantin Komarov
622e8b8e97fSKari Argillander // Here any indexed attribute can be placed.
6234534a70bSKonstantin Komarov // One of them is:
6244534a70bSKonstantin Komarov // struct ATTR_FILE_NAME AttrFileName;
6254534a70bSKonstantin Komarov //
6264534a70bSKonstantin Komarov
6274534a70bSKonstantin Komarov // The last 8 bytes of this structure contains
628e8b8e97fSKari Argillander // the VBN of subnode.
6294534a70bSKonstantin Komarov // !!! Note !!!
6304534a70bSKonstantin Komarov // This field is presented only if (flags & NTFS_IE_HAS_SUBNODES)
6314534a70bSKonstantin Komarov // __le64 vbn;
6324534a70bSKonstantin Komarov };
6334534a70bSKonstantin Komarov
6344534a70bSKonstantin Komarov static_assert(sizeof(struct NTFS_DE) == 0x10);
6354534a70bSKonstantin Komarov
de_set_vbn_le(struct NTFS_DE * e,__le64 vcn)6364534a70bSKonstantin Komarov static inline void de_set_vbn_le(struct NTFS_DE *e, __le64 vcn)
6374534a70bSKonstantin Komarov {
6384534a70bSKonstantin Komarov __le64 *v = Add2Ptr(e, le16_to_cpu(e->size) - sizeof(__le64));
6394534a70bSKonstantin Komarov
6404534a70bSKonstantin Komarov *v = vcn;
6414534a70bSKonstantin Komarov }
6424534a70bSKonstantin Komarov
de_set_vbn(struct NTFS_DE * e,CLST vcn)6434534a70bSKonstantin Komarov static inline void de_set_vbn(struct NTFS_DE *e, CLST vcn)
6444534a70bSKonstantin Komarov {
6454534a70bSKonstantin Komarov __le64 *v = Add2Ptr(e, le16_to_cpu(e->size) - sizeof(__le64));
6464534a70bSKonstantin Komarov
6474534a70bSKonstantin Komarov *v = cpu_to_le64(vcn);
6484534a70bSKonstantin Komarov }
6494534a70bSKonstantin Komarov
de_get_vbn_le(const struct NTFS_DE * e)6504534a70bSKonstantin Komarov static inline __le64 de_get_vbn_le(const struct NTFS_DE *e)
6514534a70bSKonstantin Komarov {
6524534a70bSKonstantin Komarov return *(__le64 *)Add2Ptr(e, le16_to_cpu(e->size) - sizeof(__le64));
6534534a70bSKonstantin Komarov }
6544534a70bSKonstantin Komarov
de_get_vbn(const struct NTFS_DE * e)6554534a70bSKonstantin Komarov static inline CLST de_get_vbn(const struct NTFS_DE *e)
6564534a70bSKonstantin Komarov {
6574534a70bSKonstantin Komarov __le64 *v = Add2Ptr(e, le16_to_cpu(e->size) - sizeof(__le64));
6584534a70bSKonstantin Komarov
6594534a70bSKonstantin Komarov return le64_to_cpu(*v);
6604534a70bSKonstantin Komarov }
6614534a70bSKonstantin Komarov
de_get_next(const struct NTFS_DE * e)6624534a70bSKonstantin Komarov static inline struct NTFS_DE *de_get_next(const struct NTFS_DE *e)
6634534a70bSKonstantin Komarov {
6644534a70bSKonstantin Komarov return Add2Ptr(e, le16_to_cpu(e->size));
6654534a70bSKonstantin Komarov }
6664534a70bSKonstantin Komarov
de_get_fname(const struct NTFS_DE * e)6674534a70bSKonstantin Komarov static inline struct ATTR_FILE_NAME *de_get_fname(const struct NTFS_DE *e)
6684534a70bSKonstantin Komarov {
6694534a70bSKonstantin Komarov return le16_to_cpu(e->key_size) >= SIZEOF_ATTRIBUTE_FILENAME ?
6704534a70bSKonstantin Komarov Add2Ptr(e, sizeof(struct NTFS_DE)) :
6714534a70bSKonstantin Komarov NULL;
6724534a70bSKonstantin Komarov }
6734534a70bSKonstantin Komarov
de_is_last(const struct NTFS_DE * e)6744534a70bSKonstantin Komarov static inline bool de_is_last(const struct NTFS_DE *e)
6754534a70bSKonstantin Komarov {
6764534a70bSKonstantin Komarov return e->flags & NTFS_IE_LAST;
6774534a70bSKonstantin Komarov }
6784534a70bSKonstantin Komarov
de_has_vcn(const struct NTFS_DE * e)6794534a70bSKonstantin Komarov static inline bool de_has_vcn(const struct NTFS_DE *e)
6804534a70bSKonstantin Komarov {
6814534a70bSKonstantin Komarov return e->flags & NTFS_IE_HAS_SUBNODES;
6824534a70bSKonstantin Komarov }
6834534a70bSKonstantin Komarov
de_has_vcn_ex(const struct NTFS_DE * e)6844534a70bSKonstantin Komarov static inline bool de_has_vcn_ex(const struct NTFS_DE *e)
6854534a70bSKonstantin Komarov {
6864534a70bSKonstantin Komarov return (e->flags & NTFS_IE_HAS_SUBNODES) &&
6874534a70bSKonstantin Komarov (u64)(-1) != *((u64 *)Add2Ptr(e, le16_to_cpu(e->size) -
6884534a70bSKonstantin Komarov sizeof(__le64)));
6894534a70bSKonstantin Komarov }
6904534a70bSKonstantin Komarov
6914534a70bSKonstantin Komarov #define MAX_BYTES_PER_NAME_ENTRY \
692fa3cacf5SKari Argillander ALIGN(sizeof(struct NTFS_DE) + \
6934534a70bSKonstantin Komarov offsetof(struct ATTR_FILE_NAME, name) + \
694fa3cacf5SKari Argillander NTFS_NAME_LEN * sizeof(short), 8)
6954534a70bSKonstantin Komarov
696*a0bfea5eSKonstantin Komarov #define NTFS_INDEX_HDR_HAS_SUBNODES cpu_to_le32(1)
697*a0bfea5eSKonstantin Komarov
6984534a70bSKonstantin Komarov struct INDEX_HDR {
6994534a70bSKonstantin Komarov __le32 de_off; // 0x00: The offset from the start of this structure
700e8b8e97fSKari Argillander // to the first NTFS_DE.
7014534a70bSKonstantin Komarov __le32 used; // 0x04: The size of this structure plus all
702e8b8e97fSKari Argillander // entries (quad-word aligned).
703e8b8e97fSKari Argillander __le32 total; // 0x08: The allocated size of for this structure plus all entries.
704*a0bfea5eSKonstantin Komarov __le32 flags; // 0x0C: 0x00 = Small directory, 0x01 = Large directory.
7054534a70bSKonstantin Komarov
7064534a70bSKonstantin Komarov //
7074534a70bSKonstantin Komarov // de_off + used <= total
7084534a70bSKonstantin Komarov //
7094534a70bSKonstantin Komarov };
7104534a70bSKonstantin Komarov
7114534a70bSKonstantin Komarov static_assert(sizeof(struct INDEX_HDR) == 0x10);
7124534a70bSKonstantin Komarov
hdr_first_de(const struct INDEX_HDR * hdr)7134534a70bSKonstantin Komarov static inline struct NTFS_DE *hdr_first_de(const struct INDEX_HDR *hdr)
7144534a70bSKonstantin Komarov {
7154534a70bSKonstantin Komarov u32 de_off = le32_to_cpu(hdr->de_off);
7164534a70bSKonstantin Komarov u32 used = le32_to_cpu(hdr->used);
71760ce8dfdSKonstantin Komarov struct NTFS_DE *e;
7184534a70bSKonstantin Komarov u16 esize;
7194534a70bSKonstantin Komarov
72060ce8dfdSKonstantin Komarov if (de_off >= used || de_off + sizeof(struct NTFS_DE) > used )
7214534a70bSKonstantin Komarov return NULL;
7224534a70bSKonstantin Komarov
72360ce8dfdSKonstantin Komarov e = Add2Ptr(hdr, de_off);
7244534a70bSKonstantin Komarov esize = le16_to_cpu(e->size);
7254534a70bSKonstantin Komarov if (esize < sizeof(struct NTFS_DE) || de_off + esize > used)
7264534a70bSKonstantin Komarov return NULL;
7274534a70bSKonstantin Komarov
7284534a70bSKonstantin Komarov return e;
7294534a70bSKonstantin Komarov }
7304534a70bSKonstantin Komarov
hdr_next_de(const struct INDEX_HDR * hdr,const struct NTFS_DE * e)7314534a70bSKonstantin Komarov static inline struct NTFS_DE *hdr_next_de(const struct INDEX_HDR *hdr,
7324534a70bSKonstantin Komarov const struct NTFS_DE *e)
7334534a70bSKonstantin Komarov {
7344534a70bSKonstantin Komarov size_t off = PtrOffset(hdr, e);
7354534a70bSKonstantin Komarov u32 used = le32_to_cpu(hdr->used);
7364534a70bSKonstantin Komarov u16 esize;
7374534a70bSKonstantin Komarov
7384534a70bSKonstantin Komarov if (off >= used)
7394534a70bSKonstantin Komarov return NULL;
7404534a70bSKonstantin Komarov
7414534a70bSKonstantin Komarov esize = le16_to_cpu(e->size);
7424534a70bSKonstantin Komarov
7434534a70bSKonstantin Komarov if (esize < sizeof(struct NTFS_DE) ||
7444534a70bSKonstantin Komarov off + esize + sizeof(struct NTFS_DE) > used)
7454534a70bSKonstantin Komarov return NULL;
7464534a70bSKonstantin Komarov
7474534a70bSKonstantin Komarov return Add2Ptr(e, esize);
7484534a70bSKonstantin Komarov }
7494534a70bSKonstantin Komarov
hdr_has_subnode(const struct INDEX_HDR * hdr)7504534a70bSKonstantin Komarov static inline bool hdr_has_subnode(const struct INDEX_HDR *hdr)
7514534a70bSKonstantin Komarov {
752*a0bfea5eSKonstantin Komarov return hdr->flags & NTFS_INDEX_HDR_HAS_SUBNODES;
7534534a70bSKonstantin Komarov }
7544534a70bSKonstantin Komarov
7554534a70bSKonstantin Komarov struct INDEX_BUFFER {
7564534a70bSKonstantin Komarov struct NTFS_RECORD_HEADER rhdr; // 'INDX'
7574534a70bSKonstantin Komarov __le64 vbn; // 0x10: vcn if index >= cluster or vsn id index < cluster
7584534a70bSKonstantin Komarov struct INDEX_HDR ihdr; // 0x18:
7594534a70bSKonstantin Komarov };
7604534a70bSKonstantin Komarov
7614534a70bSKonstantin Komarov static_assert(sizeof(struct INDEX_BUFFER) == 0x28);
7624534a70bSKonstantin Komarov
ib_is_empty(const struct INDEX_BUFFER * ib)7634534a70bSKonstantin Komarov static inline bool ib_is_empty(const struct INDEX_BUFFER *ib)
7644534a70bSKonstantin Komarov {
7654534a70bSKonstantin Komarov const struct NTFS_DE *first = hdr_first_de(&ib->ihdr);
7664534a70bSKonstantin Komarov
7674534a70bSKonstantin Komarov return !first || de_is_last(first);
7684534a70bSKonstantin Komarov }
7694534a70bSKonstantin Komarov
ib_is_leaf(const struct INDEX_BUFFER * ib)7704534a70bSKonstantin Komarov static inline bool ib_is_leaf(const struct INDEX_BUFFER *ib)
7714534a70bSKonstantin Komarov {
772*a0bfea5eSKonstantin Komarov return !(ib->ihdr.flags & NTFS_INDEX_HDR_HAS_SUBNODES);
7734534a70bSKonstantin Komarov }
7744534a70bSKonstantin Komarov
775e8b8e97fSKari Argillander /* Index root structure ( 0x90 ). */
7764534a70bSKonstantin Komarov enum COLLATION_RULE {
7774534a70bSKonstantin Komarov NTFS_COLLATION_TYPE_BINARY = cpu_to_le32(0),
7784534a70bSKonstantin Komarov // $I30
7794534a70bSKonstantin Komarov NTFS_COLLATION_TYPE_FILENAME = cpu_to_le32(0x01),
7804534a70bSKonstantin Komarov // $SII of $Secure and $Q of Quota
7814534a70bSKonstantin Komarov NTFS_COLLATION_TYPE_UINT = cpu_to_le32(0x10),
7824534a70bSKonstantin Komarov // $O of Quota
7834534a70bSKonstantin Komarov NTFS_COLLATION_TYPE_SID = cpu_to_le32(0x11),
7844534a70bSKonstantin Komarov // $SDH of $Secure
7854534a70bSKonstantin Komarov NTFS_COLLATION_TYPE_SECURITY_HASH = cpu_to_le32(0x12),
7864534a70bSKonstantin Komarov // $O of ObjId and "$R" for Reparse
7874534a70bSKonstantin Komarov NTFS_COLLATION_TYPE_UINTS = cpu_to_le32(0x13)
7884534a70bSKonstantin Komarov };
7894534a70bSKonstantin Komarov
7904534a70bSKonstantin Komarov static_assert(sizeof(enum COLLATION_RULE) == 4);
7914534a70bSKonstantin Komarov
7924534a70bSKonstantin Komarov //
7934534a70bSKonstantin Komarov struct INDEX_ROOT {
794e8b8e97fSKari Argillander enum ATTR_TYPE type; // 0x00: The type of attribute to index on.
795e8b8e97fSKari Argillander enum COLLATION_RULE rule; // 0x04: The rule.
796e8b8e97fSKari Argillander __le32 index_block_size;// 0x08: The size of index record.
797e8b8e97fSKari Argillander u8 index_block_clst; // 0x0C: The number of clusters or sectors per index.
7984534a70bSKonstantin Komarov u8 res[3];
7994534a70bSKonstantin Komarov struct INDEX_HDR ihdr; // 0x10:
8004534a70bSKonstantin Komarov };
8014534a70bSKonstantin Komarov
8024534a70bSKonstantin Komarov static_assert(sizeof(struct INDEX_ROOT) == 0x20);
8034534a70bSKonstantin Komarov static_assert(offsetof(struct INDEX_ROOT, ihdr) == 0x10);
8044534a70bSKonstantin Komarov
8054534a70bSKonstantin Komarov #define VOLUME_FLAG_DIRTY cpu_to_le16(0x0001)
8064534a70bSKonstantin Komarov #define VOLUME_FLAG_RESIZE_LOG_FILE cpu_to_le16(0x0002)
8074534a70bSKonstantin Komarov
8084534a70bSKonstantin Komarov struct VOLUME_INFO {
8094534a70bSKonstantin Komarov __le64 res1; // 0x00
8104534a70bSKonstantin Komarov u8 major_ver; // 0x08: NTFS major version number (before .)
8114534a70bSKonstantin Komarov u8 minor_ver; // 0x09: NTFS minor version number (after .)
8124534a70bSKonstantin Komarov __le16 flags; // 0x0A: Volume flags, see VOLUME_FLAG_XXX
8134534a70bSKonstantin Komarov
8144534a70bSKonstantin Komarov }; // sizeof=0xC
8154534a70bSKonstantin Komarov
8164534a70bSKonstantin Komarov #define SIZEOF_ATTRIBUTE_VOLUME_INFO 0xc
8174534a70bSKonstantin Komarov
8184534a70bSKonstantin Komarov #define NTFS_LABEL_MAX_LENGTH (0x100 / sizeof(short))
8194534a70bSKonstantin Komarov #define NTFS_ATTR_INDEXABLE cpu_to_le32(0x00000002)
8204534a70bSKonstantin Komarov #define NTFS_ATTR_DUPALLOWED cpu_to_le32(0x00000004)
8214534a70bSKonstantin Komarov #define NTFS_ATTR_MUST_BE_INDEXED cpu_to_le32(0x00000010)
8224534a70bSKonstantin Komarov #define NTFS_ATTR_MUST_BE_NAMED cpu_to_le32(0x00000020)
8234534a70bSKonstantin Komarov #define NTFS_ATTR_MUST_BE_RESIDENT cpu_to_le32(0x00000040)
8244534a70bSKonstantin Komarov #define NTFS_ATTR_LOG_ALWAYS cpu_to_le32(0x00000080)
8254534a70bSKonstantin Komarov
826e8b8e97fSKari Argillander /* $AttrDef file entry. */
8274534a70bSKonstantin Komarov struct ATTR_DEF_ENTRY {
828e8b8e97fSKari Argillander __le16 name[0x40]; // 0x00: Attr name.
829e8b8e97fSKari Argillander enum ATTR_TYPE type; // 0x80: struct ATTRIB type.
8304534a70bSKonstantin Komarov __le32 res; // 0x84:
8314534a70bSKonstantin Komarov enum COLLATION_RULE rule; // 0x88:
832e8b8e97fSKari Argillander __le32 flags; // 0x8C: NTFS_ATTR_XXX (see above).
833e8b8e97fSKari Argillander __le64 min_sz; // 0x90: Minimum attribute data size.
834e8b8e97fSKari Argillander __le64 max_sz; // 0x98: Maximum attribute data size.
8354534a70bSKonstantin Komarov };
8364534a70bSKonstantin Komarov
8374534a70bSKonstantin Komarov static_assert(sizeof(struct ATTR_DEF_ENTRY) == 0xa0);
8384534a70bSKonstantin Komarov
8394534a70bSKonstantin Komarov /* Object ID (0x40) */
8404534a70bSKonstantin Komarov struct OBJECT_ID {
841e8b8e97fSKari Argillander struct GUID ObjId; // 0x00: Unique Id assigned to file.
842a81f47c4SKonstantin Komarov
843a81f47c4SKonstantin Komarov // Birth Volume Id is the Object Id of the Volume on.
844e8b8e97fSKari Argillander // which the Object Id was allocated. It never changes.
845a81f47c4SKonstantin Komarov struct GUID BirthVolumeId; //0x10:
846a81f47c4SKonstantin Komarov
847a81f47c4SKonstantin Komarov // Birth Object Id is the first Object Id that was
8484534a70bSKonstantin Komarov // ever assigned to this MFT Record. I.e. If the Object Id
8494534a70bSKonstantin Komarov // is changed for some reason, this field will reflect the
8504534a70bSKonstantin Komarov // original value of the Object Id.
851a81f47c4SKonstantin Komarov struct GUID BirthObjectId; // 0x20:
852a81f47c4SKonstantin Komarov
853a81f47c4SKonstantin Komarov // Domain Id is currently unused but it is intended to be
8544534a70bSKonstantin Komarov // used in a network environment where the local machine is
8554534a70bSKonstantin Komarov // part of a Windows 2000 Domain. This may be used in a Windows
8564534a70bSKonstantin Komarov // 2000 Advanced Server managed domain.
857a81f47c4SKonstantin Komarov struct GUID DomainId; // 0x30:
8584534a70bSKonstantin Komarov };
8594534a70bSKonstantin Komarov
8604534a70bSKonstantin Komarov static_assert(sizeof(struct OBJECT_ID) == 0x40);
8614534a70bSKonstantin Komarov
8624534a70bSKonstantin Komarov /* O Directory entry structure ( rule = 0x13 ) */
8634534a70bSKonstantin Komarov struct NTFS_DE_O {
8644534a70bSKonstantin Komarov struct NTFS_DE de;
865e8b8e97fSKari Argillander struct GUID ObjId; // 0x10: Unique Id assigned to file.
866e8b8e97fSKari Argillander struct MFT_REF ref; // 0x20: MFT record number with this file.
867a81f47c4SKonstantin Komarov
868a81f47c4SKonstantin Komarov // Birth Volume Id is the Object Id of the Volume on
869e8b8e97fSKari Argillander // which the Object Id was allocated. It never changes.
870a81f47c4SKonstantin Komarov struct GUID BirthVolumeId; // 0x28:
871a81f47c4SKonstantin Komarov
872a81f47c4SKonstantin Komarov // Birth Object Id is the first Object Id that was
8734534a70bSKonstantin Komarov // ever assigned to this MFT Record. I.e. If the Object Id
8744534a70bSKonstantin Komarov // is changed for some reason, this field will reflect the
8754534a70bSKonstantin Komarov // original value of the Object Id.
876e8b8e97fSKari Argillander // This field is valid if data_size == 0x48.
877a81f47c4SKonstantin Komarov struct GUID BirthObjectId; // 0x38:
878a81f47c4SKonstantin Komarov
879a81f47c4SKonstantin Komarov // Domain Id is currently unused but it is intended
8804534a70bSKonstantin Komarov // to be used in a network environment where the local
8814534a70bSKonstantin Komarov // machine is part of a Windows 2000 Domain. This may be
8824534a70bSKonstantin Komarov // used in a Windows 2000 Advanced Server managed domain.
883a81f47c4SKonstantin Komarov struct GUID BirthDomainId; // 0x48:
8844534a70bSKonstantin Komarov };
8854534a70bSKonstantin Komarov
8864534a70bSKonstantin Komarov static_assert(sizeof(struct NTFS_DE_O) == 0x58);
8874534a70bSKonstantin Komarov
8884534a70bSKonstantin Komarov /* Q Directory entry structure ( rule = 0x11 ) */
8894534a70bSKonstantin Komarov struct NTFS_DE_Q {
8904534a70bSKonstantin Komarov struct NTFS_DE de;
8914534a70bSKonstantin Komarov __le32 owner_id; // 0x10: Unique Id assigned to file
892a81f47c4SKonstantin Komarov
893a81f47c4SKonstantin Komarov /* here is 0x30 bytes of user quota. NOTE: 4 byte aligned! */
8944534a70bSKonstantin Komarov __le32 Version; // 0x14: 0x02
895a81f47c4SKonstantin Komarov __le32 Flags; // 0x18: Quota flags, see above
8964534a70bSKonstantin Komarov __le64 BytesUsed; // 0x1C:
8974534a70bSKonstantin Komarov __le64 ChangeTime; // 0x24:
8984534a70bSKonstantin Komarov __le64 WarningLimit; // 0x28:
8994534a70bSKonstantin Komarov __le64 HardLimit; // 0x34:
9004534a70bSKonstantin Komarov __le64 ExceededTime; // 0x3C:
9014534a70bSKonstantin Komarov
9024534a70bSKonstantin Komarov // SID is placed here
903a81f47c4SKonstantin Komarov }__packed; // sizeof() = 0x44
9044534a70bSKonstantin Komarov
905a81f47c4SKonstantin Komarov static_assert(sizeof(struct NTFS_DE_Q) == 0x44);
9064534a70bSKonstantin Komarov
9074534a70bSKonstantin Komarov #define SecurityDescriptorsBlockSize 0x40000 // 256K
9084534a70bSKonstantin Komarov #define SecurityDescriptorMaxSize 0x20000 // 128K
9094534a70bSKonstantin Komarov #define Log2OfSecurityDescriptorsBlockSize 18
9104534a70bSKonstantin Komarov
9114534a70bSKonstantin Komarov struct SECURITY_KEY {
9124534a70bSKonstantin Komarov __le32 hash; // Hash value for descriptor
9134534a70bSKonstantin Komarov __le32 sec_id; // Security Id (guaranteed unique)
9144534a70bSKonstantin Komarov };
9154534a70bSKonstantin Komarov
9164534a70bSKonstantin Komarov /* Security descriptors (the content of $Secure::SDS data stream) */
9174534a70bSKonstantin Komarov struct SECURITY_HDR {
918e8b8e97fSKari Argillander struct SECURITY_KEY key; // 0x00: Security Key.
919e8b8e97fSKari Argillander __le64 off; // 0x08: Offset of this entry in the file.
920e8b8e97fSKari Argillander __le32 size; // 0x10: Size of this entry, 8 byte aligned.
921e8b8e97fSKari Argillander /*
922e8b8e97fSKari Argillander * Security descriptor itself is placed here.
923e8b8e97fSKari Argillander * Total size is 16 byte aligned.
924e8b8e97fSKari Argillander */
9254534a70bSKonstantin Komarov } __packed;
9264534a70bSKonstantin Komarov
927a81f47c4SKonstantin Komarov static_assert(sizeof(struct SECURITY_HDR) == 0x14);
9284534a70bSKonstantin Komarov
9294534a70bSKonstantin Komarov /* SII Directory entry structure */
9304534a70bSKonstantin Komarov struct NTFS_DE_SII {
9314534a70bSKonstantin Komarov struct NTFS_DE de;
9324534a70bSKonstantin Komarov __le32 sec_id; // 0x10: Key: sizeof(security_id) = wKeySize
9334534a70bSKonstantin Komarov struct SECURITY_HDR sec_hdr; // 0x14:
9344534a70bSKonstantin Komarov } __packed;
9354534a70bSKonstantin Komarov
936a81f47c4SKonstantin Komarov static_assert(offsetof(struct NTFS_DE_SII, sec_hdr) == 0x14);
937a81f47c4SKonstantin Komarov static_assert(sizeof(struct NTFS_DE_SII) == 0x28);
9384534a70bSKonstantin Komarov
9394534a70bSKonstantin Komarov /* SDH Directory entry structure */
9404534a70bSKonstantin Komarov struct NTFS_DE_SDH {
9414534a70bSKonstantin Komarov struct NTFS_DE de;
9424534a70bSKonstantin Komarov struct SECURITY_KEY key; // 0x10: Key
9434534a70bSKonstantin Komarov struct SECURITY_HDR sec_hdr; // 0x18: Data
9444534a70bSKonstantin Komarov __le16 magic[2]; // 0x2C: 0x00490049 "I I"
9454534a70bSKonstantin Komarov };
9464534a70bSKonstantin Komarov
9474534a70bSKonstantin Komarov #define SIZEOF_SDH_DIRENTRY 0x30
9484534a70bSKonstantin Komarov
9494534a70bSKonstantin Komarov struct REPARSE_KEY {
9504534a70bSKonstantin Komarov __le32 ReparseTag; // 0x00: Reparse Tag
9514534a70bSKonstantin Komarov struct MFT_REF ref; // 0x04: MFT record number with this file
9524534a70bSKonstantin Komarov }; // sizeof() = 0x0C
9534534a70bSKonstantin Komarov
9544534a70bSKonstantin Komarov static_assert(offsetof(struct REPARSE_KEY, ref) == 0x04);
9554534a70bSKonstantin Komarov #define SIZEOF_REPARSE_KEY 0x0C
9564534a70bSKonstantin Komarov
9574534a70bSKonstantin Komarov /* Reparse Directory entry structure */
9584534a70bSKonstantin Komarov struct NTFS_DE_R {
9594534a70bSKonstantin Komarov struct NTFS_DE de;
960e8b8e97fSKari Argillander struct REPARSE_KEY key; // 0x10: Reparse Key.
961e8b8e97fSKari Argillander u32 zero; // 0x1c:
9624534a70bSKonstantin Komarov }; // sizeof() = 0x20
9634534a70bSKonstantin Komarov
9644534a70bSKonstantin Komarov static_assert(sizeof(struct NTFS_DE_R) == 0x20);
9654534a70bSKonstantin Komarov
9664534a70bSKonstantin Komarov /* CompressReparseBuffer.WofVersion */
9674534a70bSKonstantin Komarov #define WOF_CURRENT_VERSION cpu_to_le32(1)
9684534a70bSKonstantin Komarov /* CompressReparseBuffer.WofProvider */
9694534a70bSKonstantin Komarov #define WOF_PROVIDER_WIM cpu_to_le32(1)
9704534a70bSKonstantin Komarov /* CompressReparseBuffer.WofProvider */
9714534a70bSKonstantin Komarov #define WOF_PROVIDER_SYSTEM cpu_to_le32(2)
9724534a70bSKonstantin Komarov /* CompressReparseBuffer.ProviderVer */
9734534a70bSKonstantin Komarov #define WOF_PROVIDER_CURRENT_VERSION cpu_to_le32(1)
9744534a70bSKonstantin Komarov
9754534a70bSKonstantin Komarov #define WOF_COMPRESSION_XPRESS4K cpu_to_le32(0) // 4k
9764534a70bSKonstantin Komarov #define WOF_COMPRESSION_LZX32K cpu_to_le32(1) // 32k
9774534a70bSKonstantin Komarov #define WOF_COMPRESSION_XPRESS8K cpu_to_le32(2) // 8k
9784534a70bSKonstantin Komarov #define WOF_COMPRESSION_XPRESS16K cpu_to_le32(3) // 16k
9794534a70bSKonstantin Komarov
9804534a70bSKonstantin Komarov /*
9814534a70bSKonstantin Komarov * ATTR_REPARSE (0xC0)
9824534a70bSKonstantin Komarov *
9834534a70bSKonstantin Komarov * The reparse struct GUID structure is used by all 3rd party layered drivers to
9844534a70bSKonstantin Komarov * store data in a reparse point. For non-Microsoft tags, The struct GUID field
9854534a70bSKonstantin Komarov * cannot be GUID_NULL.
9864534a70bSKonstantin Komarov * The constraints on reparse tags are defined below.
9874534a70bSKonstantin Komarov * Microsoft tags can also be used with this format of the reparse point buffer.
9884534a70bSKonstantin Komarov */
9894534a70bSKonstantin Komarov struct REPARSE_POINT {
9904534a70bSKonstantin Komarov __le32 ReparseTag; // 0x00:
9914534a70bSKonstantin Komarov __le16 ReparseDataLength;// 0x04:
9924534a70bSKonstantin Komarov __le16 Reserved;
9934534a70bSKonstantin Komarov
9944534a70bSKonstantin Komarov struct GUID Guid; // 0x08:
9954534a70bSKonstantin Komarov
9964534a70bSKonstantin Komarov //
9974534a70bSKonstantin Komarov // Here GenericReparseBuffer is placed
9984534a70bSKonstantin Komarov //
9994534a70bSKonstantin Komarov };
10004534a70bSKonstantin Komarov
10014534a70bSKonstantin Komarov static_assert(sizeof(struct REPARSE_POINT) == 0x18);
10024534a70bSKonstantin Komarov
1003e8b8e97fSKari Argillander /* Maximum allowed size of the reparse data. */
10044534a70bSKonstantin Komarov #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE (16 * 1024)
10054534a70bSKonstantin Komarov
1006e8b8e97fSKari Argillander /*
1007e8b8e97fSKari Argillander * The value of the following constant needs to satisfy the following
1008e8b8e97fSKari Argillander * conditions:
1009e8b8e97fSKari Argillander * (1) Be at least as large as the largest of the reserved tags.
1010e8b8e97fSKari Argillander * (2) Be strictly smaller than all the tags in use.
1011e8b8e97fSKari Argillander */
10124534a70bSKonstantin Komarov #define IO_REPARSE_TAG_RESERVED_RANGE 1
10134534a70bSKonstantin Komarov
1014e8b8e97fSKari Argillander /*
1015e8b8e97fSKari Argillander * The reparse tags are a ULONG. The 32 bits are laid out as follows:
1016e8b8e97fSKari Argillander *
1017e8b8e97fSKari Argillander * 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
1018e8b8e97fSKari Argillander * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
1019e8b8e97fSKari Argillander * +-+-+-+-+-----------------------+-------------------------------+
1020e8b8e97fSKari Argillander * |M|R|N|R| Reserved bits | Reparse Tag Value |
1021e8b8e97fSKari Argillander * +-+-+-+-+-----------------------+-------------------------------+
1022e8b8e97fSKari Argillander *
1023e8b8e97fSKari Argillander * M is the Microsoft bit. When set to 1, it denotes a tag owned by Microsoft.
1024e8b8e97fSKari Argillander * All ISVs must use a tag with a 0 in this position.
1025e8b8e97fSKari Argillander * Note: If a Microsoft tag is used by non-Microsoft software, the
1026e8b8e97fSKari Argillander * behavior is not defined.
1027e8b8e97fSKari Argillander *
1028e8b8e97fSKari Argillander * R is reserved. Must be zero for non-Microsoft tags.
1029e8b8e97fSKari Argillander *
1030e8b8e97fSKari Argillander * N is name surrogate. When set to 1, the file represents another named
1031e8b8e97fSKari Argillander * entity in the system.
1032e8b8e97fSKari Argillander *
1033e8b8e97fSKari Argillander * The M and N bits are OR-able.
1034e8b8e97fSKari Argillander * The following macros check for the M and N bit values:
1035e8b8e97fSKari Argillander */
10364534a70bSKonstantin Komarov
1037e8b8e97fSKari Argillander /*
1038e8b8e97fSKari Argillander * Macro to determine whether a reparse point tag corresponds to a tag
1039e8b8e97fSKari Argillander * owned by Microsoft.
1040e8b8e97fSKari Argillander */
10414534a70bSKonstantin Komarov #define IsReparseTagMicrosoft(_tag) (((_tag)&IO_REPARSE_TAG_MICROSOFT))
10424534a70bSKonstantin Komarov
1043e8b8e97fSKari Argillander /* Macro to determine whether a reparse point tag is a name surrogate. */
10444534a70bSKonstantin Komarov #define IsReparseTagNameSurrogate(_tag) (((_tag)&IO_REPARSE_TAG_NAME_SURROGATE))
10454534a70bSKonstantin Komarov
1046e8b8e97fSKari Argillander /*
1047e8b8e97fSKari Argillander * The following constant represents the bits that are valid to use in
1048e8b8e97fSKari Argillander * reparse tags.
1049e8b8e97fSKari Argillander */
10504534a70bSKonstantin Komarov #define IO_REPARSE_TAG_VALID_VALUES 0xF000FFFF
10514534a70bSKonstantin Komarov
1052e8b8e97fSKari Argillander /*
1053e8b8e97fSKari Argillander * Macro to determine whether a reparse tag is a valid tag.
1054e8b8e97fSKari Argillander */
10554534a70bSKonstantin Komarov #define IsReparseTagValid(_tag) \
10564534a70bSKonstantin Komarov (!((_tag) & ~IO_REPARSE_TAG_VALID_VALUES) && \
10574534a70bSKonstantin Komarov ((_tag) > IO_REPARSE_TAG_RESERVED_RANGE))
10584534a70bSKonstantin Komarov
1059e8b8e97fSKari Argillander /* Microsoft tags for reparse points. */
10604534a70bSKonstantin Komarov
10614534a70bSKonstantin Komarov enum IO_REPARSE_TAG {
10624534a70bSKonstantin Komarov IO_REPARSE_TAG_SYMBOLIC_LINK = cpu_to_le32(0),
10634534a70bSKonstantin Komarov IO_REPARSE_TAG_NAME_SURROGATE = cpu_to_le32(0x20000000),
10644534a70bSKonstantin Komarov IO_REPARSE_TAG_MICROSOFT = cpu_to_le32(0x80000000),
10654534a70bSKonstantin Komarov IO_REPARSE_TAG_MOUNT_POINT = cpu_to_le32(0xA0000003),
10664534a70bSKonstantin Komarov IO_REPARSE_TAG_SYMLINK = cpu_to_le32(0xA000000C),
10674534a70bSKonstantin Komarov IO_REPARSE_TAG_HSM = cpu_to_le32(0xC0000004),
10684534a70bSKonstantin Komarov IO_REPARSE_TAG_SIS = cpu_to_le32(0x80000007),
10694534a70bSKonstantin Komarov IO_REPARSE_TAG_DEDUP = cpu_to_le32(0x80000013),
10704534a70bSKonstantin Komarov IO_REPARSE_TAG_COMPRESS = cpu_to_le32(0x80000017),
10714534a70bSKonstantin Komarov
1072e8b8e97fSKari Argillander /*
1073e8b8e97fSKari Argillander * The reparse tag 0x80000008 is reserved for Microsoft internal use.
1074e8b8e97fSKari Argillander * May be published in the future.
1075e8b8e97fSKari Argillander */
10764534a70bSKonstantin Komarov
1077e8b8e97fSKari Argillander /* Microsoft reparse tag reserved for DFS */
10784534a70bSKonstantin Komarov IO_REPARSE_TAG_DFS = cpu_to_le32(0x8000000A),
10794534a70bSKonstantin Komarov
1080e8b8e97fSKari Argillander /* Microsoft reparse tag reserved for the file system filter manager. */
10814534a70bSKonstantin Komarov IO_REPARSE_TAG_FILTER_MANAGER = cpu_to_le32(0x8000000B),
10824534a70bSKonstantin Komarov
1083e8b8e97fSKari Argillander /* Non-Microsoft tags for reparse points */
10844534a70bSKonstantin Komarov
1085e8b8e97fSKari Argillander /* Tag allocated to CONGRUENT, May 2000. Used by IFSTEST. */
10864534a70bSKonstantin Komarov IO_REPARSE_TAG_IFSTEST_CONGRUENT = cpu_to_le32(0x00000009),
10874534a70bSKonstantin Komarov
1088e8b8e97fSKari Argillander /* Tag allocated to ARKIVIO. */
10894534a70bSKonstantin Komarov IO_REPARSE_TAG_ARKIVIO = cpu_to_le32(0x0000000C),
10904534a70bSKonstantin Komarov
1091e8b8e97fSKari Argillander /* Tag allocated to SOLUTIONSOFT. */
10924534a70bSKonstantin Komarov IO_REPARSE_TAG_SOLUTIONSOFT = cpu_to_le32(0x2000000D),
10934534a70bSKonstantin Komarov
1094e8b8e97fSKari Argillander /* Tag allocated to COMMVAULT. */
10954534a70bSKonstantin Komarov IO_REPARSE_TAG_COMMVAULT = cpu_to_le32(0x0000000E),
10964534a70bSKonstantin Komarov
1097e8b8e97fSKari Argillander /* OneDrive?? */
10984534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD = cpu_to_le32(0x9000001A),
10994534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_1 = cpu_to_le32(0x9000101A),
11004534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_2 = cpu_to_le32(0x9000201A),
11014534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_3 = cpu_to_le32(0x9000301A),
11024534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_4 = cpu_to_le32(0x9000401A),
11034534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_5 = cpu_to_le32(0x9000501A),
11044534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_6 = cpu_to_le32(0x9000601A),
11054534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_7 = cpu_to_le32(0x9000701A),
11064534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_8 = cpu_to_le32(0x9000801A),
11074534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_9 = cpu_to_le32(0x9000901A),
11084534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_A = cpu_to_le32(0x9000A01A),
11094534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_B = cpu_to_le32(0x9000B01A),
11104534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_C = cpu_to_le32(0x9000C01A),
11114534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_D = cpu_to_le32(0x9000D01A),
11124534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_E = cpu_to_le32(0x9000E01A),
11134534a70bSKonstantin Komarov IO_REPARSE_TAG_CLOUD_F = cpu_to_le32(0x9000F01A),
11144534a70bSKonstantin Komarov
11154534a70bSKonstantin Komarov };
11164534a70bSKonstantin Komarov
11174534a70bSKonstantin Komarov #define SYMLINK_FLAG_RELATIVE 1
11184534a70bSKonstantin Komarov
11194534a70bSKonstantin Komarov /* Microsoft reparse buffer. (see DDK for details) */
11204534a70bSKonstantin Komarov struct REPARSE_DATA_BUFFER {
11214534a70bSKonstantin Komarov __le32 ReparseTag; // 0x00:
11224534a70bSKonstantin Komarov __le16 ReparseDataLength; // 0x04:
11234534a70bSKonstantin Komarov __le16 Reserved;
11244534a70bSKonstantin Komarov
11254534a70bSKonstantin Komarov union {
1126e8b8e97fSKari Argillander /* If ReparseTag == 0xA0000003 (IO_REPARSE_TAG_MOUNT_POINT) */
11274534a70bSKonstantin Komarov struct {
11284534a70bSKonstantin Komarov __le16 SubstituteNameOffset; // 0x08
11294534a70bSKonstantin Komarov __le16 SubstituteNameLength; // 0x0A
11304534a70bSKonstantin Komarov __le16 PrintNameOffset; // 0x0C
11314534a70bSKonstantin Komarov __le16 PrintNameLength; // 0x0E
11324534a70bSKonstantin Komarov __le16 PathBuffer[]; // 0x10
11334534a70bSKonstantin Komarov } MountPointReparseBuffer;
11344534a70bSKonstantin Komarov
1135e8b8e97fSKari Argillander /*
1136e8b8e97fSKari Argillander * If ReparseTag == 0xA000000C (IO_REPARSE_TAG_SYMLINK)
1137e8b8e97fSKari Argillander * https://msdn.microsoft.com/en-us/library/cc232006.aspx
1138e8b8e97fSKari Argillander */
11394534a70bSKonstantin Komarov struct {
11404534a70bSKonstantin Komarov __le16 SubstituteNameOffset; // 0x08
11414534a70bSKonstantin Komarov __le16 SubstituteNameLength; // 0x0A
11424534a70bSKonstantin Komarov __le16 PrintNameOffset; // 0x0C
11434534a70bSKonstantin Komarov __le16 PrintNameLength; // 0x0E
11444534a70bSKonstantin Komarov // 0-absolute path 1- relative path, SYMLINK_FLAG_RELATIVE
11454534a70bSKonstantin Komarov __le32 Flags; // 0x10
11464534a70bSKonstantin Komarov __le16 PathBuffer[]; // 0x14
11474534a70bSKonstantin Komarov } SymbolicLinkReparseBuffer;
11484534a70bSKonstantin Komarov
1149e8b8e97fSKari Argillander /* If ReparseTag == 0x80000017U */
11504534a70bSKonstantin Komarov struct {
11514534a70bSKonstantin Komarov __le32 WofVersion; // 0x08 == 1
1152e8b8e97fSKari Argillander /*
1153e8b8e97fSKari Argillander * 1 - WIM backing provider ("WIMBoot"),
11544534a70bSKonstantin Komarov * 2 - System compressed file provider
11554534a70bSKonstantin Komarov */
1156e8b8e97fSKari Argillander __le32 WofProvider; // 0x0C:
11574534a70bSKonstantin Komarov __le32 ProviderVer; // 0x10: == 1 WOF_FILE_PROVIDER_CURRENT_VERSION == 1
11584534a70bSKonstantin Komarov __le32 CompressionFormat; // 0x14: 0, 1, 2, 3. See WOF_COMPRESSION_XXX
11594534a70bSKonstantin Komarov } CompressReparseBuffer;
11604534a70bSKonstantin Komarov
11614534a70bSKonstantin Komarov struct {
1162e8b8e97fSKari Argillander u8 DataBuffer[1]; // 0x08:
11634534a70bSKonstantin Komarov } GenericReparseBuffer;
11644534a70bSKonstantin Komarov };
11654534a70bSKonstantin Komarov };
11664534a70bSKonstantin Komarov
11674534a70bSKonstantin Komarov /* ATTR_EA_INFO (0xD0) */
11684534a70bSKonstantin Komarov
11694534a70bSKonstantin Komarov #define FILE_NEED_EA 0x80 // See ntifs.h
1170e8b8e97fSKari Argillander /*
1171e8b8e97fSKari Argillander * FILE_NEED_EA, indicates that the file to which the EA belongs cannot be
11724534a70bSKonstantin Komarov * interpreted without understanding the associated extended attributes.
11734534a70bSKonstantin Komarov */
11744534a70bSKonstantin Komarov struct EA_INFO {
1175e8b8e97fSKari Argillander __le16 size_pack; // 0x00: Size of buffer to hold in packed form.
1176e8b8e97fSKari Argillander __le16 count; // 0x02: Count of EA's with FILE_NEED_EA bit set.
1177e8b8e97fSKari Argillander __le32 size; // 0x04: Size of buffer to hold in unpacked form.
11784534a70bSKonstantin Komarov };
11794534a70bSKonstantin Komarov
11804534a70bSKonstantin Komarov static_assert(sizeof(struct EA_INFO) == 8);
11814534a70bSKonstantin Komarov
11824534a70bSKonstantin Komarov /* ATTR_EA (0xE0) */
11834534a70bSKonstantin Komarov struct EA_FULL {
11844534a70bSKonstantin Komarov __le32 size; // 0x00: (not in packed)
1185e8b8e97fSKari Argillander u8 flags; // 0x04:
1186e8b8e97fSKari Argillander u8 name_len; // 0x05:
1187e8b8e97fSKari Argillander __le16 elength; // 0x06:
1188e8b8e97fSKari Argillander u8 name[]; // 0x08:
11894534a70bSKonstantin Komarov };
11904534a70bSKonstantin Komarov
11914534a70bSKonstantin Komarov static_assert(offsetof(struct EA_FULL, name) == 8);
11924534a70bSKonstantin Komarov
11934534a70bSKonstantin Komarov #define ACL_REVISION 2
11944534a70bSKonstantin Komarov #define ACL_REVISION_DS 4
11954534a70bSKonstantin Komarov
11964534a70bSKonstantin Komarov #define SE_SELF_RELATIVE cpu_to_le16(0x8000)
11974534a70bSKonstantin Komarov
11984534a70bSKonstantin Komarov struct SECURITY_DESCRIPTOR_RELATIVE {
11994534a70bSKonstantin Komarov u8 Revision;
12004534a70bSKonstantin Komarov u8 Sbz1;
12014534a70bSKonstantin Komarov __le16 Control;
12024534a70bSKonstantin Komarov __le32 Owner;
12034534a70bSKonstantin Komarov __le32 Group;
12044534a70bSKonstantin Komarov __le32 Sacl;
12054534a70bSKonstantin Komarov __le32 Dacl;
12064534a70bSKonstantin Komarov };
12074534a70bSKonstantin Komarov static_assert(sizeof(struct SECURITY_DESCRIPTOR_RELATIVE) == 0x14);
12084534a70bSKonstantin Komarov
12094534a70bSKonstantin Komarov struct ACE_HEADER {
12104534a70bSKonstantin Komarov u8 AceType;
12114534a70bSKonstantin Komarov u8 AceFlags;
12124534a70bSKonstantin Komarov __le16 AceSize;
12134534a70bSKonstantin Komarov };
12144534a70bSKonstantin Komarov static_assert(sizeof(struct ACE_HEADER) == 4);
12154534a70bSKonstantin Komarov
12164534a70bSKonstantin Komarov struct ACL {
12174534a70bSKonstantin Komarov u8 AclRevision;
12184534a70bSKonstantin Komarov u8 Sbz1;
12194534a70bSKonstantin Komarov __le16 AclSize;
12204534a70bSKonstantin Komarov __le16 AceCount;
12214534a70bSKonstantin Komarov __le16 Sbz2;
12224534a70bSKonstantin Komarov };
12234534a70bSKonstantin Komarov static_assert(sizeof(struct ACL) == 8);
12244534a70bSKonstantin Komarov
12254534a70bSKonstantin Komarov struct SID {
12264534a70bSKonstantin Komarov u8 Revision;
12274534a70bSKonstantin Komarov u8 SubAuthorityCount;
12284534a70bSKonstantin Komarov u8 IdentifierAuthority[6];
12294534a70bSKonstantin Komarov __le32 SubAuthority[];
12304534a70bSKonstantin Komarov };
12314534a70bSKonstantin Komarov static_assert(offsetof(struct SID, SubAuthority) == 8);
12324534a70bSKonstantin Komarov
123387790b65SKari Argillander #endif /* _LINUX_NTFS3_NTFS_H */
12344534a70bSKonstantin Komarov // clang-format on
1235