aboutsummaryrefslogtreecommitdiffstats
path: root/core/fs/ntfs/ntfs.h
blob: 735693621177a6540e4b5223047e34aa3eb2b3f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
 * Copyright (c) Paulo Alcantara <pcacjr@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111EXIT_FAILURE307, USA.
*/

#ifndef _NTFS_H_
#define _NTFS_H_

struct ntfs_bpb {
    uint8_t jmp_boot[3];
    char oem_name[8];
    uint16_t sector_size;
    uint8_t sec_per_clust;
    uint16_t res_sectors;
    uint8_t zero_0[3];
    uint16_t zero_1;
    uint8_t media;
    uint16_t zero_2;
    uint16_t unused_0;
    uint16_t unused_1;
    uint32_t unused_2;
    uint32_t zero_3;
    uint32_t unused_3;
    uint64_t total_sectors;
    uint64_t mft_lclust;
    uint64_t mft_mirr_lclust;
    int8_t clust_per_mft_record;
    uint8_t unused_4[3];
    uint8_t clust_per_idx_buf;
    uint8_t unused_5[3];
    uint64_t vol_serial;
    uint32_t unused_6;

    uint8_t pad[428];       /* padding to a sector boundary (512 bytes) */
} __attribute__((__packed__));

struct ntfs_sb_info {
    block_t mft_block;              /* The first MFT record block */

    unsigned mft_size;              /* The MFT size in sectors */
    uint64_t mft_record_size;       /* MFT record size in bytes */

    unsigned long long clusters;    /* Total number of clusters */

    unsigned clust_shift;           /* Based on sectors */
    unsigned clust_byte_shift;      /* Based on bytes */
    unsigned clust_mask;
    unsigned clust_size;

} __attribute__((__packed__));

/* The NTFS in-memory inode structure */
struct ntfs_inode {
    int64_t initialized_size;
    int64_t allocated_size;
    unsigned long mft_no;   /* Number of the mft record / inode */
    uint16_t seq_no;        /* Sequence number of the mft record */
    uint32_t type;          /* Attribute type of this inode */
    uint8_t non_resident;
    bool in_idx_root;
    uint32_t idx_blocks_count;
    uint32_t entries_count;
    int64_t last_vcn;
    union {                 /* Non-resident $DATA attribute */
        struct {            /* Used only if non_resident flags isn't set */
            uint32_t offset;    /* Data offset */
        } resident;
        struct {            /* Used only if non_resident is set */
            uint64_t len;
            int64_t lcn;        /* Logical Cluster Number offset */
        } non_resident;
    } data;
    union {
        struct {    /* It is a directory, $MFT, or an index inode */
            uint32_t block_size;
            uint32_t vcn_size;
            uint32_t collation_rule;
            uint8_t block_size_shift;    /* log2 of the above */
            uint8_t vcn_size_shift;      /* log2 of the above */
        } index;
    } itype;
    uint32_t start_cluster; /* Starting cluster address */
    sector_t start;         /* Starting sector */
    sector_t offset;        /* Current sector offset */
    sector_t here;          /* Sector corresponding to offset */
};

enum {
    MAP_UNSPEC,
    MAP_START           = 1 << 0,
    MAP_END             = 1 << 1,
    MAP_ALLOCATED       = 1 << 2,
    MAP_UNALLOCATED     = 1 << 3,
    MAP_MASK            = 0x0000000F,
};

struct mapping_chunk {
    uint64_t len;
    int64_t lcn;        /* Logical Cluster Number */
    uint32_t flags;     /* Specific flags of this chunk */
};

/* System defined attributes (32-bit)
 * Each attribute type has a corresponding attribute name (in Unicode)
 */
enum {
    NTFS_AT_UNUSED                      = 0x00,
    NTFS_AT_STANDARD_INFORMATION        = 0x10,
    NTFS_AT_ATTR_LIST                   = 0x20,
    NTFS_AT_FILENAME                    = 0x30,
    NTFS_AT_OBJ_ID                      = 0x40,
    NTFS_AT_SECURITY_DESCP              = 0x50,
    NTFS_AT_VOL_NAME                    = 0x60,
    NTFS_AT_VOL_INFO                    = 0x70,
    NTFS_AT_DATA                        = 0x80,
    NTFS_AT_INDEX_ROOT                  = 0x90,
    NTFS_AT_INDEX_ALLOCATION            = 0xA0,
    NTFS_AT_BITMAP                      = 0xB0,
    NTFS_AT_REPARSE_POINT               = 0xC0,
    NTFS_AT_EA_INFO                     = 0xD0,
    NTFS_AT_EA                          = 0xE0,
    NTFS_AT_PROPERTY_SET                = 0xF0,
    NTFS_AT_LOGGED_UTIL_STREAM          = 0x100,
    NTFS_AT_FIRST_USER_DEFINED_ATTR     = 0x1000,
    NTFS_AT_END                         = 0xFFFFFFFF,
};

/* NTFS File Permissions (also called attributes in DOS terminology) */
enum {
    NTFS_FILE_ATTR_READONLY                     = 0x00000001,
    NTFS_FILE_ATTR_HIDDEN                       = 0x00000002,
    NTFS_FILE_ATTR_SYSTEM                       = 0x00000004,
    NTFS_FILE_ATTR_DIRECTORY                    = 0x00000010,
    NTFS_FILE_ATTR_ARCHIVE                      = 0x00000020,
    NTFS_FILE_ATTR_DEVICE                       = 0x00000040,
    NTFS_FILE_ATTR_NORMAL                       = 0x00000080,
    NTFS_FILE_ATTR_TEMPORARY                    = 0x00000100,
    NTFS_FILE_ATTR_SPARSE_FILE                  = 0x00000200,
    NTFS_FILE_ATTR_REPARSE_POINT                = 0x00000400,
    NTFS_FILE_ATTR_COMPRESSED                   = 0x00000800,
    NTFS_FILE_ATTR_OFFLINE                      = 0x00001000,
    NTFS_FILE_ATTR_NOT_CONTENT_INDEXED          = 0x00002000,
    NTFS_FILE_ATTR_ENCRYPTED                    = 0x00004000,
    NTFS_FILE_ATTR_VALID_FLAGS                  = 0x00007FB7,
    NTFS_FILE_ATTR_VALID_SET_FLAGS              = 0x000031A7,
    NTFS_FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT  = 0x10000000,
    NTFS_FILE_ATTR_DUP_VIEW_INDEX_PRESENT       = 0x20000000,
};

/* The collation rules for sorting views/indexes/etc (32-bit) */
enum {
    NTFS_COLLATION_BINARY               = 0x00,
    NTFS_COLLATION_FILE_NAME            = 0x01,
    NTFS_COLLATION_UNICODE_STRING       = 0x02,
    NTFS_COLLATION_NTOFS_ULONG          = 0x10,
    NTFS_COLLATION_NTOFS_SID            = 0x11,
    NTFS_COLLATION_NTOFS_SECURITY_HASH  = 0x12,
    NTFS_COLLATION_NTOFS_ULONGS         = 0x13,
};

/*
 * Magic identifiers present at the beginning of all ntfs record containing
 * records (like mft records for example).
 */
enum {
    /* Found in $MFT/$DATA */
    NTFS_MAGIC_FILE     = 0x454C4946,   /* MFT entry */
    NTFS_MAGIC_INDX     = 0x58444E49,   /* Index buffer */
    NTFS_MAGIC_HOLE     = 0x454C4F48,

    /* Found in $LogFile/$DATA */
    NTFS_MAGIC_RSTR     = 0x52545352,
    NTFS_MAGIC_RCRD     = 0x44524352,
    /* Found in $LogFile/$DATA (May be found in $MFT/$DATA, also ?) */
    NTFS_MAGIC_CHKDSK   = 0x444B4843,
    /* Found in all ntfs record containing records. */
    NTFS_MAGIC_BAAD     = 0x44414142,
    NTFS_MAGIC_EMPTY    = 0xFFFFFFFF,   /* Record is empty */
};

typedef struct {
    uint32_t magic;
    uint16_t usa_ofs;
    uint16_t usa_count;
} __attribute__((__packed__)) NTFS_RECORD;

/* The $MFT metadata file types */
typedef enum {
    FILE_MFT            = 0,
    FILE_MFTMirr        = 1,
    FILE_LogFile        = 2,
    FILE_Volume         = 3,
    FILE_AttrDef        = 4,
    FILE_root           = 5,
    FILE_Bitmap         = 6,
    FILE_Boot           = 7,
    FILE_BadClus        = 8,
    FILE_Secure         = 9,
    FILE_UpCase         = 10,
    FILE_Extend         = 11,
    FILE_reserved12     = 12,
    FILE_reserved13     = 13,
    FILE_reserved14     = 14,
    FILE_reserved15     = 15,
    FILE_reserved16     = 16,
} NTFS_SYSTEM_FILES;

enum {
    MFT_RECORD_IN_USE       = 0x0001,
    MFT_RECORD_IS_DIRECTORY = 0x0002,
} __attribute__((__packed__));

typedef struct {
    uint32_t magic;
    uint16_t usa_ofs;
    uint16_t usa_count;
    uint64_t lsn;
    uint16_t seq_no;
    uint16_t link_count;
    uint16_t attrs_offset;
    uint16_t flags;     /* MFT record flags */
    uint32_t bytes_in_use;
    uint32_t bytes_allocated;
    uint64_t base_mft_record;
    uint16_t next_attr_instance;
    uint16_t reserved;
    uint32_t mft_record_no;
} __attribute__((__packed__)) MFT_RECORD;   /* 48 bytes */

/* This is the version without the NTFS 3.1+ specific fields */
typedef struct {
    uint32_t magic;
    uint16_t usa_ofs;
    uint16_t usa_count;
    uint64_t lsn;
    uint16_t seq_no;
    uint16_t link_count;
    uint16_t attrs_offset;
    uint16_t flags;     /* MFT record flags */
    uint32_t bytes_in_use;
    uint32_t bytes_allocated;
    uint64_t base_mft_record;
    uint16_t next_attr_instance;
} __attribute__((__packed__)) MFT_RECORD_OLD;   /* 42 bytes */

enum {
    ATTR_DEF_INDEXABLE          = 0x02,
    ATTR_DEF_MULTIPLE           = 0x04,
    ATTR_DEF_NOT_ZERO           = 0x08,
    ATTR_DEF_INDEXED_UNIQUE     = 0x10,
    ATTR_DEF_NAMED_UNIQUE       = 0x20,
    ATTR_DEF_RESIDENT           = 0x40,
    ATTR_DEF_ALWAYS_LOG         = 0x80,
};

typedef struct {
    uint16_t name[0x40];
    uint32_t type;
    uint32_t display_rule;
    uint32_t collation_rule;
    uint32_t flags;     /* Attr def flags */
    uint64_t min_size;
    uint64_t max_size;
} __attribute__((__packed__)) ATTR_DEF;

/* Attribute flags (16-bit) */
enum {
    ATTR_IS_COMPRESSED      = 0x0001,
    ATTR_COMPRESSION_MASK   = 0x00FF,

    ATTR_IS_ENCRYPTED       = 0x4000,
    ATTR_IS_SPARSE          = 0x8000,
} __attribute__((__packed__));

/* Flags of resident attributes (8-bit) */
enum {
    RESIDENT_ATTR_IS_INDEXED = 0x01,
} __attribute__((__packed__));

typedef struct {
    uint32_t type;      /* Attr. type code */
    uint32_t len;
    uint8_t non_resident;
    uint8_t name_len;
    uint16_t name_offset;
    uint16_t flags;     /* Attr. flags */
    uint16_t instance;
    union {
        struct {    /* Resident attribute */
            uint32_t value_len;
            uint16_t value_offset;
            uint8_t flags;  /* Flags of resident attributes */
            int8_t reserved;
        } __attribute__((__packed__)) resident;
        struct {    /* Non-resident attributes */
            uint64_t lowest_vcn;
            uint64_t highest_vcn;
            uint16_t mapping_pairs_offset;
            uint8_t compression_unit;
            uint8_t reserved[5];
            int64_t allocated_size;
            int64_t initialized_size;
            int64_t compressed_size;
        } __attribute__((__packed__)) non_resident;
    } __attribute__((__packed__)) data;
} __attribute__((__packed__)) ATTR_RECORD;

/* Attribute: Standard Information (0x10)
 * Note: always resident
 */
typedef struct {
    int64_t ctime;
    int64_t atime;
    int64_t mtime;
    int64_t rtime;
    uint32_t file_attrs;
    union {
        struct {    /* NTFS 1.2 (48 bytes) */
            uint8_t reserved12[12];
        } __attribute__((__packed__)) v1;
        struct {    /* NTFS 3.x (72 bytes) */
            uint32_t max_version;
            uint32_t version;
            uint32_t class_id;
            uint32_t owner_id;
            uint32_t sec_id;
            uint64_t quota_charged;
            int64_t usn;
        } __attribute__((__packed__)) v3;
    } __attribute__((__packed__)) ver;
} __attribute__((__packed__)) STANDARD_INFORMATION;

/* Attribute: Attribute List (0x20)
 * Note: can be either resident or non-resident
 */
typedef struct {
    uint32_t type;
    uint16_t len;
    uint8_t name_len;
    uint8_t name_offset;
    uint64_t lowest_vcn;
    uint64_t mft_ref;
    uint16_t instance;
    uint16_t name[0];       /* Name in Unicode */
    /* sizeof() = 26 + (attribute_name_length * 2) bytes */
} __attribute__((__packed__)) ATTR_LIST_ENTRY;

#define NTFS_MAX_FILE_NAME_LEN 255

/* Possible namespaces for filenames in ntfs (8-bit) */
enum {
    FILE_NAME_POSIX             = 0x00,
    FILE_NAME_WIN32             = 0x01,
    FILE_NAME_DOS               = 0x02,
    FILE_NAME_WIN32_AND_DOS     = 0x03,
} __attribute__((__packed__));

/* Attribute: Filename (0x30)
 * Note: always resident
 */
typedef struct {
    uint64_t parent_directory;
    int64_t ctime;
    int64_t atime;
    int64_t mtime;
    int64_t rtime;
    uint64_t allocated_size;
    uint64_t data_size;
    uint32_t file_attrs;
    union {
        struct {
            uint16_t __packed___ea_size;
            uint16_t reserved;      /* reserved for alignment */
        } __attribute__((__packed__)) ea;
        struct {
            uint32_t reparse_point_tag;
        } __attribute__((__packed__)) rp;
    } __attribute__((__packed__)) type;
    uint8_t file_name_len;
    uint8_t file_name_type;
    uint16_t file_name[0];          /* File name in Unicode */
} __attribute__((__packed__)) FILE_NAME_ATTR;

/* Attribute: Volume Name (0x60)
 * Note: always resident
 * Note: Present only in FILE_volume
 */
typedef struct {
    uint16_t name[0];       /* The name of the volume in Unicode */
} __attribute__((__packed__)) VOLUME_NAME;

/* Volume flags (16-bit) */
enum {
    VOLUME_IS_DIRTY             = 0x0001,
    VOLUME_RESIZE_LOG_FILE      = 0x0002,
    VOLUME_UPGRADE_ON_MOUNT     = 0x0004,
    VOLUME_MOUNTED_ON_NT4       = 0x0008,

    VOLUME_DELETE_USN_UNDERWAY  = 0x0010,
    VOLUME_REPAIR_OBJECT_ID     = 0x0020,

    VOLUME_CHKDSK_UNDERWAY      = 0x4000,
    VOLUME_MODIFIED_BY_CHKDSK   = 0x8000,

    VOLUME_FLAGS_MASK           = 0xC03F,

    VOLUME_MUST_MOUNT_RO_MASK   = 0xC027,
} __attribute__((__packed__));

/* Attribute: Volume Information (0x70)
 * Note: always resident
 * Note: present only in FILE_Volume
 */
typedef struct {
    uint64_t reserved;
    uint8_t major_ver;
    uint8_t minor_ver;
    uint16_t flags;     /* Volume flags */
} __attribute__((__packed__)) VOLUME_INFORMATION;

/* Attribute: Data attribute (0x80)
 * Note: can be either resident or non-resident
 */
typedef struct {
    uint8_t data[0];
} __attribute__((__packed__)) DATA_ATTR;

/* Index header flags (8-bit) */
enum {
    SMALL_INDEX = 0,
    LARGE_INDEX = 1,
    LEAF_NODE   = 0,
    INDEX_NODE  = 1,
    NODE_MASK   = 1,
} __attribute__((__packed__));

/* Header for the indexes, describing the INDEX_ENTRY records, which
 * follow the INDEX_HEADER.
 */
typedef struct {
    uint32_t entries_offset;
    uint32_t index_len;
    uint32_t allocated_size;
    uint8_t flags;              /* Index header flags */
    uint8_t reserved[3];        /* Align to 8-byte boundary */
} __attribute__((__packed__)) INDEX_HEADER;

/* Attribute: Index Root (0x90)
 * Note: always resident
 */
typedef struct {
    uint32_t type;  /* It is $FILE_NAME for directories, zero for view indexes.
                     * No other values allowed.
                     */
    uint32_t collation_rule;
    uint32_t index_block_size;
    uint8_t clust_per_index_block;
    uint8_t reserved[3];
    INDEX_HEADER index;
} __attribute__((__packed__)) INDEX_ROOT;

/* Attribute: Index allocation (0xA0)
 * Note: always non-resident, of course! :-)
 */
typedef struct {
    uint32_t magic;
    uint16_t usa_ofs;           /* Update Sequence Array offsets */
    uint16_t usa_count;         /* Update Sequence Array number in bytes */
    int64_t lsn;
    int64_t index_block_vcn;    /* Virtual cluster number of the index block */
    INDEX_HEADER index;
} __attribute__((__packed__)) INDEX_BLOCK;

typedef INDEX_BLOCK INDEX_ALLOCATION;

enum {
    INDEX_ENTRY_NODE            = 1,
    INDEX_ENTRY_END             = 2,
    /* force enum bit width to 16-bit */
    INDEX_ENTRY_SPACE_FILTER    = 0xFFFF,
} __attribute__((__packed__));

typedef struct {
    union {
        struct { /* Only valid when INDEX_ENTRY_END is not set */
            uint64_t indexed_file;
        } __attribute__((__packed__)) dir;
        struct { /* Used for views/indexes to find the entry's data */
            uint16_t data_offset;
            uint16_t data_len;
            uint32_t reservedV;
        } __attribute__((__packed__)) vi;
    } __attribute__((__packed__)) data;
    uint16_t len;
    uint16_t key_len;
    uint16_t flags;     /* Index entry flags */
    uint16_t reserved;  /* Align to 8-byte boundary */
} __attribute__((__packed__)) INDEX_ENTRY_HEADER;

typedef struct {
    union {
        struct { /* Only valid when INDEX_ENTRY_END is not set */
            uint64_t indexed_file;
        } __attribute__((__packed__)) dir;
        struct { /* Used for views/indexes to find the entry's data */
            uint16_t data_offset;
            uint16_t data_len;
            uint32_t reservedV;
        } __attribute__((__packed__)) vi;
    } __attribute__((__packed__)) data;
    uint16_t len;
    uint16_t key_len;
    uint16_t flags;     /* Index entry flags */
    uint16_t reserved;  /* Align to 8-byte boundary */
    union {
        FILE_NAME_ATTR file_name;
        //SII_INDEX_KEY sii;
        //SDH_INDEX_KEY sdh;
        //GUID object_id;
        //REPARSE_INDEX_KEY reparse;
        //SID sid;
        uint32_t owner_id;
    } __attribute__((__packed__)) key;
} __attribute__((__packed__)) INDEX_ENTRY;

typedef struct {
    uint8_t bitmap[0];      /* Array of bits */
} __attribute__((__packed__)) BITMAP_ATTR;

static inline struct ntfs_sb_info *NTFS_SB(struct fs_info *fs)
{
    return fs->fs_info;
}

#define NTFS_PVT(i) ((struct ntfs_inode *)((i)->pvt))

#endif /* _NTFS_H_ */