-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlayout.h
More file actions
2346 lines (2230 loc) · 96.4 KB
/
layout.h
File metadata and controls
2346 lines (2230 loc) · 96.4 KB
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* All NTFS associated on-disk structures.
*
* Copyright (c) 2001-2005 Anton Altaparmakov
* Copyright (c) 2002 Richard Russon
*/
#ifndef _LINUX_NTFS_LAYOUT_H
#define _LINUX_NTFS_LAYOUT_H
#include <linux/types.h>
#include <linux/bitops.h>
#include <linux/list.h>
#include <asm/byteorder.h>
/* The NTFS oem_id "NTFS " */
#define magicNTFS cpu_to_le64(0x202020205346544eULL)
/*
* Location of bootsector on partition:
* The standard NTFS_BOOT_SECTOR is on sector 0 of the partition.
* On NT4 and above there is one backup copy of the boot sector to
* be found on the last sector of the partition (not normally accessible
* from within Windows as the bootsector contained number of sectors
* value is one less than the actual value!).
* On versions of NT 3.51 and earlier, the backup copy was located at
* number of sectors/2 (integer divide), i.e. in the middle of the volume.
*/
/*
* BIOS parameter block (bpb) structure.
*
* @bytes_per_sector: Size of a sector in bytes (usually 512).
* Matches the logical sector size of the underlying device.
* @sectors_per_cluster: Size of a cluster in sectors (NTFS cluster size / sector size).
* @reserved_sectors: Number of reserved sectors at the beginning of the volume.
* Always set to 0 in NTFS.
* @fats: Number of FAT tables.
* Always 0 in NTFS (no FAT tables exist).
* @root_entries: Number of entries in the root directory.
* Always 0 in NTFS.
* @sectors: Total number of sectors on the volume.
* Always 0 in NTFS (use @large_sectors instead).
* @media_type: Media descriptor byte.
* 0xF8 for hard disk (fixed media) in NTFS.
* @sectors_per_fat: Number of sectors per FAT table.
* Always 0 in NTFS.
* @sectors_per_track: Number of sectors per track.
* Irrelevant for NTFS.
* @heads: Number of heads (CHS geometry).
* Irrelevant for NTFS.
* @hidden_sectors: Number of hidden sectors before the start of the partition.
* Always 0 in NTFS boot sector.
* @large_sectors: Total number of sectors on the volume.
*/
struct bios_parameter_block {
__le16 bytes_per_sector;
u8 sectors_per_cluster;
__le16 reserved_sectors;
u8 fats;
__le16 root_entries;
__le16 sectors;
u8 media_type;
__le16 sectors_per_fat;
__le16 sectors_per_track;
__le16 heads;
__le32 hidden_sectors;
__le32 large_sectors;
} __packed;
/*
* NTFS boot sector structure.
*
* @jump: 3-byte jump instruction to boot code (irrelevant for NTFS).
* Typically 0xEB 0x52 0x90 or similar.
* @oem_id: OEM identifier string (8 bytes).
* Always "NTFS " (with trailing spaces) in NTFS volumes.
* @bpb: Legacy BIOS Parameter Block (see struct bios_parameter_block).
* Mostly zeroed or set to fixed values for NTFS compatibility.
* @unused: 4 bytes, reserved/unused.
* NTFS disk editors show it as:
* - physical_drive (0x80 for fixed disk)
* - current_head (0)
* - extended_boot_signature (0x80 or 0x28)
* - unused (0)
* Always zero in practice for NTFS.
* @number_of_sectors: Number of sectors in volume. Gives maximum volume
* size of 2^63 sectors. Assuming standard sector
* size of 512 bytes, the maximum byte size is
* approx. 4.7x10^21 bytes. (-;
* @mft_lcn: Logical cluster number (LCN) of the $MFT data attribute.
* Location of the Master File Table.
* @mftmirr_lcn: LCN of the $MFTMirr (first 3-4 MFT records copy).
* Mirror for boot-time recovery.
* @clusters_per_mft_record:
* Size of each MFT record in clusters.
* @reserved0: 3 bytes, reserved/zero.
* @clusters_per_index_record:
* Size of each index block/record in clusters.
* @reserved1: 3 bytes, reserved/zero.
* @volume_serial_number:
* 64-bit volume serial number.
* Used for identification (irrelevant for NTFS operation).
* @checksum: 32-bit checksum of the boot sector (excluding this field).
* Used to detect boot sector corruption.
* @bootstrap: 426 bytes of bootstrap code.
* Irrelevant for NTFS (contains x86 boot loader stub).
* @end_of_sector_marker:
* 2-byte end-of-sector signature.
* Always 0xAA55 (little-endian magic number).
*/
struct ntfs_boot_sector {
u8 jump[3];
__le64 oem_id;
struct bios_parameter_block bpb;
u8 unused[4];
__le64 number_of_sectors;
__le64 mft_lcn;
__le64 mftmirr_lcn;
s8 clusters_per_mft_record;
u8 reserved0[3];
s8 clusters_per_index_record;
u8 reserved1[3];
__le64 volume_serial_number;
__le32 checksum;
u8 bootstrap[426];
__le16 end_of_sector_marker;
} __packed;
static_assert(sizeof(struct ntfs_boot_sector) == 512);
/*
* Magic identifiers present at the beginning of all ntfs record containing
* records (like mft records for example).
*
* magic_FILE: MFT entry header ("FILE" in ASCII).
* Used in $MFT/$DATA for all master file table records.
* magic_INDX: Index buffer header ("INDX" in ASCII).
* Used in $INDEX_ALLOCATION attributes (directories, $I30 indexes).
* magic_HOLE: Hole marker ("HOLE" in ASCII).
* Introduced in NTFS 3.0+, used for sparse/hole regions in some contexts.
* magic_RSTR: Restart page header ("RSTR" in ASCII).
* Used in LogFile for restart pages (transaction log recovery).
* magic_RCRD: Log record page header ("RCRD" in ASCII).
* Used in LogFile for individual log record pages.
* magic_CHKD: Chkdsk modified marker ("CHKD" in ASCII).
* Set by chkdsk when it modifies a record; indicates repair was done.
* magic_BAAD: Bad record marker ("BAAD" in ASCII).
* Indicates a multi-sector transfer failure was detected.
* The record is corrupted/unusable; often set during I/O errors.
* magic_empty: Empty/uninitialized page marker (0xffffffff).
* Used in LogFile when a page is filled with 0xff bytes
* and has not yet been initialized. Must be formatted before use.
*/
enum {
magic_FILE = cpu_to_le32(0x454c4946),
magic_INDX = cpu_to_le32(0x58444e49),
magic_HOLE = cpu_to_le32(0x454c4f48),
magic_RSTR = cpu_to_le32(0x52545352),
magic_RCRD = cpu_to_le32(0x44524352),
magic_CHKD = cpu_to_le32(0x444b4843),
magic_BAAD = cpu_to_le32(0x44414142),
magic_empty = cpu_to_le32(0xffffffff)
};
/*
* Generic magic comparison macros. Finally found a use for the ## preprocessor
* operator! (-8
*/
static inline bool __ntfs_is_magic(__le32 x, __le32 r)
{
return (x == r);
}
#define ntfs_is_magic(x, m) __ntfs_is_magic(x, magic_##m)
static inline bool __ntfs_is_magicp(__le32 *p, __le32 r)
{
return (*p == r);
}
#define ntfs_is_magicp(p, m) __ntfs_is_magicp(p, magic_##m)
/*
* Specialised magic comparison macros for the NTFS_RECORD_TYPEs defined above.
*/
#define ntfs_is_file_record(x) (ntfs_is_magic(x, FILE))
#define ntfs_is_file_recordp(p) (ntfs_is_magicp(p, FILE))
#define ntfs_is_mft_record(x) (ntfs_is_file_record(x))
#define ntfs_is_mft_recordp(p) (ntfs_is_file_recordp(p))
#define ntfs_is_indx_record(x) (ntfs_is_magic(x, INDX))
#define ntfs_is_indx_recordp(p) (ntfs_is_magicp(p, INDX))
#define ntfs_is_hole_record(x) (ntfs_is_magic(x, HOLE))
#define ntfs_is_hole_recordp(p) (ntfs_is_magicp(p, HOLE))
#define ntfs_is_rstr_record(x) (ntfs_is_magic(x, RSTR))
#define ntfs_is_rstr_recordp(p) (ntfs_is_magicp(p, RSTR))
#define ntfs_is_rcrd_record(x) (ntfs_is_magic(x, RCRD))
#define ntfs_is_rcrd_recordp(p) (ntfs_is_magicp(p, RCRD))
#define ntfs_is_chkd_record(x) (ntfs_is_magic(x, CHKD))
#define ntfs_is_chkd_recordp(p) (ntfs_is_magicp(p, CHKD))
#define ntfs_is_baad_record(x) (ntfs_is_magic(x, BAAD))
#define ntfs_is_baad_recordp(p) (ntfs_is_magicp(p, BAAD))
#define ntfs_is_empty_record(x) (ntfs_is_magic(x, empty))
#define ntfs_is_empty_recordp(p) (ntfs_is_magicp(p, empty))
/*
* struct ntfs_record - Common header for all multi-sector protected NTFS records
*
* @magic: 4-byte magic identifier for the record type and/or status.
* Common values are defined in the magic_* enum (FILE, INDX, RSTR,
* RCRD, CHKD, BAAD, HOLE, empty).
* - "FILE" = MFT record
* - "INDX" = Index allocation block
* - "BAAD" = Record corrupted (multi-sector fixup failed)
* - 0xffffffff = Uninitialized/empty page
* @usa_ofs: Offset (in bytes) from the start of this record to the Update
* Sequence Array (USA).
* The USA is located at record + usa_ofs.
* @usa_count: Number of 16-bit entries in the USA array (including the Update
* Sequence Number itself).
* - Number of fixup locations = usa_count - 1
* - Each fixup location is a 16-bit value in the record that needs
* protection against torn writes.
*
* The Update Sequence Array (usa) is an array of the __le16 values which belong
* to the end of each sector protected by the update sequence record in which
* this array is contained. Note that the first entry is the Update Sequence
* Number (usn), a cyclic counter of how many times the protected record has
* been written to disk. The values 0 and -1 (ie. 0xffff) are not used. All
* last le16's of each sector have to be equal to the usn (during reading) or
* are set to it (during writing). If they are not, an incomplete multi sector
* transfer has occurred when the data was written.
* The maximum size for the update sequence array is fixed to:
* maximum size = usa_ofs + (usa_count * 2) = 510 bytes
* The 510 bytes comes from the fact that the last __le16 in the array has to
* (obviously) finish before the last __le16 of the first 512-byte sector.
* This formula can be used as a consistency check in that usa_ofs +
* (usa_count * 2) has to be less than or equal to 510.
*/
struct ntfs_record {
__le32 magic;
__le16 usa_ofs;
__le16 usa_count;
} __packed;
/*
* System files mft record numbers. All these files are always marked as used
* in the bitmap attribute of the mft; presumably in order to avoid accidental
* allocation for random other mft records. Also, the sequence number for each
* of the system files is always equal to their mft record number and it is
* never modified.
*
* FILE_MFT: Master File Table (MFT) itself.
* Data attribute contains all MFT entries;
* Bitmap attribute tracks which records are in use (bit==1).
* FILE_MFTMirr: MFT mirror: copy of the first four (or more) MFT records
* in its data attribute.
* If cluster size > 4 KiB, copies first N records where
* N = cluster_size / mft_record_size.
* FILE_LogFile: Journaling log (LogFile) in data attribute.
* Used for transaction logging and recovery.
* FILE_Volume: Volume information and name.
* Contains $VolumeName (label) and $VolumeInformation
* (flags, NTFS version). Windows calls this the volume DASD.
* FILE_AttrDef: Attribute definitions array in data attribute.
* Defines all possible attribute types and their properties.
* FILE_root: Root directory ($Root).
* The top-level directory of the filesystem.
* FILE_Bitmap: Cluster allocation bitmap ($Bitmap) in data attribute.
* Tracks free/used clusters (LCNs) on the volume.
* FILE_Boot: Boot sector ($Boot) in data attribute.
* Always located at cluster 0; contains BPB and NTFS parameters.
* FILE_BadClus: Bad cluster list ($BadClus) in non-resident data attribute.
* Marks all known bad clusters.
* FILE_Secure: Security descriptors ($Secure).
* Contains shared $SDS (security descriptors) and two indexes
* ($SDH, $SII). Introduced in Windows 2000.
* Before that, it was called $Quota but was unused.
* FILE_UpCase: Uppercase table ($UpCase) in data attribute.
* Maps all 65536 Unicode characters to their uppercase forms.
* FILE_Extend: System directory ($Extend).
* Contains additional system files ($ObjId, $Quota, $Reparse,
* $UsnJrnl, etc.). Introduced in NTFS 3.0 (Windows 2000).
* FILE_reserved12: Reserved for future use (MFT records 12–15).
* FILE_reserved13: Reserved.
* FILE_reserved14: Reserved.
* FILE_reserved15: Reserved.
* FILE_first_user: First possible user-created file MFT record number.
* Used as a boundary to distinguish system files from user files.
*/
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_first_user = 16,
};
/*
* enum - Flags for MFT record header
*
* These are the so far known MFT_RECORD_* flags (16-bit) which contain
* information about the mft record in which they are present.
*
* MFT_RECORD_IN_USE: This MFT record is allocated and in use.
* (bit set = record is valid/used; clear = free)
* MFT_RECORD_IS_DIRECTORY: This MFT record represents a directory.
* (Used to quickly distinguish files from directories)
* MFT_RECORD_IS_4: Indicates the record is a special "record 4" type.
* (Rarely used; related to NTFS internal special cases,
* often for $AttrDef or early system files)
* MFT_RECORD_IS_VIEW_INDEX: This MFT record is used as a view index.
* (Specific to NTFS indexed views or object ID indexes)
* MFT_REC_SPACE_FILLER: Dummy value to force the enum to be 16-bit wide.
* (Not a real flag; just a sentinel to ensure the type
* is __le16 and no higher bits are accidentally used)
*/
enum {
MFT_RECORD_IN_USE = cpu_to_le16(0x0001),
MFT_RECORD_IS_DIRECTORY = cpu_to_le16(0x0002),
MFT_RECORD_IS_4 = cpu_to_le16(0x0004),
MFT_RECORD_IS_VIEW_INDEX = cpu_to_le16(0x0008),
MFT_REC_SPACE_FILLER = cpu_to_le16(0xffff), /*Just to make flags 16-bit.*/
} __packed;
/*
* mft references (aka file references or file record segment references) are
* used whenever a structure needs to refer to a record in the mft.
*
* A reference consists of a 48-bit index into the mft and a 16-bit sequence
* number used to detect stale references.
*
* For error reporting purposes we treat the 48-bit index as a signed quantity.
*
* The sequence number is a circular counter (skipping 0) describing how many
* times the referenced mft record has been (re)used. This has to match the
* sequence number of the mft record being referenced, otherwise the reference
* is considered stale and removed.
*
* If the sequence number is zero it is assumed that no sequence number
* consistency checking should be performed.
*/
/*
* Define two unpacking macros to get to the reference (MREF) and
* sequence number (MSEQNO) respectively.
* The _LE versions are to be applied on little endian MFT_REFs.
* Note: The _LE versions will return a CPU endian formatted value!
*/
#define MFT_REF_MASK_CPU 0x0000ffffffffffffULL
#define MFT_REF_MASK_LE cpu_to_le64(MFT_REF_MASK_CPU)
#define MK_MREF(m, s) ((u64)(((u64)(s) << 48) | \
((u64)(m) & MFT_REF_MASK_CPU)))
#define MK_LE_MREF(m, s) cpu_to_le64(MK_MREF(m, s))
#define MREF(x) ((unsigned long)((x) & MFT_REF_MASK_CPU))
#define MSEQNO(x) ((u16)(((x) >> 48) & 0xffff))
#define MREF_LE(x) ((unsigned long)(le64_to_cpu(x) & MFT_REF_MASK_CPU))
#define MREF_INO(x) ((unsigned long)MREF_LE(x))
#define MSEQNO_LE(x) ((u16)((le64_to_cpu(x) >> 48) & 0xffff))
#define IS_ERR_MREF(x) (((x) & 0x0000800000000000ULL) ? true : false)
#define ERR_MREF(x) ((u64)((s64)(x)))
#define MREF_ERR(x) ((int)((s64)(x)))
/*
* struct mft_record - NTFS Master File Table (MFT) record header
*
* The mft record header present at the beginning of every record in the mft.
* This is followed by a sequence of variable length attribute records which
* is terminated by an attribute of type AT_END which is a truncated attribute
* in that it only consists of the attribute type code AT_END and none of the
* other members of the attribute structure are present.
*
* magic: Record magic ("FILE" for valid MFT entries).
* See ntfs_record magic enum for other values.
* usa_ofs: Offset to Update Sequence Array (see ntfs_record).
* usa_count: Number of entries in USA (see ntfs_record).
* lsn: Log sequence number (LSN) from LogFile.
* Incremented on every modification to this record.
* sequence_number: Reuse count of this MFT record slot.
* Incremented (skipping zero) when the file is deleted.
* Zero means never reused or special case.
* Part of MFT reference (together with record number).
* link_count: Number of hard links (directory entries) to this file.
* Only meaningful in base MFT records.
* When deleting a directory entry:
* - If link_count == 1, delete the whole file
* - Else remove only the $FILE_NAME attribute and decrement
* attrs_offset: Byte offset from start of MFT record to first attribute.
* Must be 8-byte aligned.
* flags: Bit array of MFT_RECORD_* flags (see MFT_RECORD_IN_USE enum).
* MFT_RECORD_IN_USE cleared when record is freed/deleted.
* bytes_in_use: Number of bytes actually used in this MFT record.
* Must be 8-byte aligned.
* Includes header + all attributes + padding.
* bytes_allocated: Total allocated size of this MFT record.
* Usually equal to MFT record size (1024 bytes or cluster size).
* base_mft_record: MFT reference to the base record.
* 0 for base records.
* Non-zero for extension records → points to base record
* containing the $ATTRIBUTE_LIST that describes this extension.
* next_attr_instance: Next attribute instance number to assign.
* Incremented after each use.
* Reset to 0 when MFT record is reused.
* First instance is always 0.
* reserved: Reserved for alignment (NTFS 3.1+).
* mft_record_number: This MFT record's number (index in $MFT).
* Only present in NTFS 3.1+ (Windows XP and above).
*/
struct mft_record {
__le32 magic;
__le16 usa_ofs;
__le16 usa_count;
__le64 lsn;
__le16 sequence_number;
__le16 link_count;
__le16 attrs_offset;
__le16 flags;
__le32 bytes_in_use;
__le32 bytes_allocated;
__le64 base_mft_record;
__le16 next_attr_instance;
__le16 reserved;
__le32 mft_record_number;
} __packed;
static_assert(sizeof(struct mft_record) == 48);
/**x
* struct mft_record_old - Old NTFS MFT record header (pre-NTFS 3.1 / Windows XP)
*
* This is the older version of the MFT record header used in NTFS versions
* prior to 3.1 (Windows XP and later). It lacks the additional fields
* @reserved and @mft_record_number that were added in NTFS 3.1+.
*
* @magic: Record magic ("FILE" for valid MFT entries).
* See ntfs_record magic enum for other values.
* @usa_ofs: Offset to Update Sequence Array (see ntfs_record).
* @usa_count: Number of entries in USA (see ntfs_record).
* @lsn: Log sequence number (LSN) from LogFile.
* Incremented on every modification to this record.
* @sequence_number: Reuse count of this MFT record slot.
* Incremented (skipping zero) when the file is deleted.
* Zero means never reused or special case.
* Part of MFT reference (together with record number).
* @link_count: Number of hard links (directory entries) to this file.
* Only meaningful in base MFT records.
* When deleting a directory entry:
* - If link_count == 1, delete the whole file
* - Else remove only the $FILE_NAME attribute and decrement
* @attrs_offset: Byte offset from start of MFT record to first attribute.
* Must be 8-byte aligned.
* @flags: Bit array of MFT_RECORD_* flags (see MFT_RECORD_IN_USE enum).
* MFT_RECORD_IN_USE cleared when record is freed/deleted.
* @bytes_in_use: Number of bytes actually used in this MFT record.
* Must be 8-byte aligned.
* Includes header + all attributes + padding.
* @bytes_allocated: Total allocated size of this MFT record.
* Usually equal to MFT record size (1024 bytes or cluster size).
* @base_mft_record: MFT reference to the base record.
* 0 for base records.
* Non-zero for extension records → points to base record
* containing the $ATTRIBUTE_LIST that describes this extension.
* @next_attr_instance: Next attribute instance number to assign.
* Incremented after each use.
* Reset to 0 when MFT record is reused.
* First instance is always 0.
*/
struct mft_record_old {
__le32 magic;
__le16 usa_ofs;
__le16 usa_count;
__le64 lsn;
__le16 sequence_number;
__le16 link_count;
__le16 attrs_offset;
__le16 flags;
__le32 bytes_in_use;
__le32 bytes_allocated;
__le64 base_mft_record;
__le16 next_attr_instance;
} __packed;
static_assert(sizeof(struct mft_record_old) == 42);
/*
* System defined attributes (32-bit). Each attribute type has a corresponding
* attribute name (Unicode string of maximum 64 character length) as described
* by the attribute definitions present in the data attribute of the $AttrDef
* system file. On NTFS 3.0 volumes the names are just as the types are named
* in the below defines exchanging AT_ for the dollar sign ($). If that is not
* a revealing choice of symbol I do not know what is... (-;
*/
enum {
AT_UNUSED = cpu_to_le32(0),
AT_STANDARD_INFORMATION = cpu_to_le32(0x10),
AT_ATTRIBUTE_LIST = cpu_to_le32(0x20),
AT_FILE_NAME = cpu_to_le32(0x30),
AT_OBJECT_ID = cpu_to_le32(0x40),
AT_SECURITY_DESCRIPTOR = cpu_to_le32(0x50),
AT_VOLUME_NAME = cpu_to_le32(0x60),
AT_VOLUME_INFORMATION = cpu_to_le32(0x70),
AT_DATA = cpu_to_le32(0x80),
AT_INDEX_ROOT = cpu_to_le32(0x90),
AT_INDEX_ALLOCATION = cpu_to_le32(0xa0),
AT_BITMAP = cpu_to_le32(0xb0),
AT_REPARSE_POINT = cpu_to_le32(0xc0),
AT_EA_INFORMATION = cpu_to_le32(0xd0),
AT_EA = cpu_to_le32(0xe0),
AT_PROPERTY_SET = cpu_to_le32(0xf0),
AT_LOGGED_UTILITY_STREAM = cpu_to_le32(0x100),
AT_FIRST_USER_DEFINED_ATTRIBUTE = cpu_to_le32(0x1000),
AT_END = cpu_to_le32(0xffffffff)
};
/*
* The collation rules for sorting views/indexes/etc (32-bit).
*
* COLLATION_BINARY - Collate by binary compare where the first byte is most
* significant.
* COLLATION_UNICODE_STRING - Collate Unicode strings by comparing their binary
* Unicode values, except that when a character can be uppercased, the
* upper case value collates before the lower case one.
* COLLATION_FILE_NAME - Collate file names as Unicode strings. The collation
* is done very much like COLLATION_UNICODE_STRING. In fact I have no idea
* what the difference is. Perhaps the difference is that file names
* would treat some special characters in an odd way (see
* unistr.c::ntfs_collate_names() and unistr.c::legal_ansi_char_array[]
* for what I mean but COLLATION_UNICODE_STRING would not give any special
* treatment to any characters at all, but this is speculation.
* COLLATION_NTOFS_ULONG - Sorting is done according to ascending __le32 key
* values. E.g. used for $SII index in FILE_Secure, which sorts by
* security_id (le32).
* COLLATION_NTOFS_SID - Sorting is done according to ascending SID values.
* E.g. used for $O index in FILE_Extend/$Quota.
* COLLATION_NTOFS_SECURITY_HASH - Sorting is done first by ascending hash
* values and second by ascending security_id values. E.g. used for $SDH
* index in FILE_Secure.
* COLLATION_NTOFS_ULONGS - Sorting is done according to a sequence of ascending
* __le32 key values. E.g. used for $O index in FILE_Extend/$ObjId, which
* sorts by object_id (16-byte), by splitting up the object_id in four
* __le32 values and using them as individual keys. E.g. take the following
* two security_ids, stored as follows on disk:
* 1st: a1 61 65 b7 65 7b d4 11 9e 3d 00 e0 81 10 42 59
* 2nd: 38 14 37 d2 d2 f3 d4 11 a5 21 c8 6b 79 b1 97 45
* To compare them, they are split into four __le32 values each, like so:
* 1st: 0xb76561a1 0x11d47b65 0xe0003d9e 0x59421081
* 2nd: 0xd2371438 0x11d4f3d2 0x6bc821a5 0x4597b179
* Now, it is apparent why the 2nd object_id collates after the 1st: the
* first __le32 value of the 1st object_id is less than the first __le32 of
* the 2nd object_id. If the first __le32 values of both object_ids were
* equal then the second __le32 values would be compared, etc.
*/
enum {
COLLATION_BINARY = cpu_to_le32(0x00),
COLLATION_FILE_NAME = cpu_to_le32(0x01),
COLLATION_UNICODE_STRING = cpu_to_le32(0x02),
COLLATION_NTOFS_ULONG = cpu_to_le32(0x10),
COLLATION_NTOFS_SID = cpu_to_le32(0x11),
COLLATION_NTOFS_SECURITY_HASH = cpu_to_le32(0x12),
COLLATION_NTOFS_ULONGS = cpu_to_le32(0x13),
};
/*
* enum - Attribute definition flags
*
* The flags (32-bit) describing attribute properties in the attribute
* definition structure.
* The INDEXABLE flag is fairly certainly correct as only the file
* name attribute has this flag set and this is the only attribute indexed in
* NT4.
*
* ATTR_DEF_INDEXABLE: Attribute can be indexed.
* (Used for creating indexes like $I30, $SDH, etc.)
* ATTR_DEF_MULTIPLE: Attribute type can be present multiple times
* in the MFT record of an inode.
* (e.g., multiple $FILE_NAME, $DATA streams)
* ATTR_DEF_NOT_ZERO: Attribute value must contain at least one non-zero byte.
* (Prevents empty or all-zero values)
* ATTR_DEF_INDEXED_UNIQUE: Attribute must be indexed and the value must be unique
* for this attribute type across all MFT records of an inode.
* (e.g., security descriptor IDs in $Secure)
* ATTR_DEF_NAMED_UNIQUE: Attribute must be named and the name must be unique
* for this attribute type across all MFT records of an inode.
* (e.g., named $DATA streams or alternate data streams)
* ATTR_DEF_RESIDENT: Attribute must be resident (stored in MFT record).
* (Cannot be non-resident/sparse/compressed)
* ATTR_DEF_ALWAYS_LOG: Always log modifications to this attribute in LogFile,
* regardless of whether it is resident or non-resident.
* Without this flag, modifications are logged only if resident.
* (Used for critical metadata attributes)
*/
enum {
ATTR_DEF_INDEXABLE = cpu_to_le32(0x02),
ATTR_DEF_MULTIPLE = cpu_to_le32(0x04),
ATTR_DEF_NOT_ZERO = cpu_to_le32(0x08),
ATTR_DEF_INDEXED_UNIQUE = cpu_to_le32(0x10),
ATTR_DEF_NAMED_UNIQUE = cpu_to_le32(0x20),
ATTR_DEF_RESIDENT = cpu_to_le32(0x40),
ATTR_DEF_ALWAYS_LOG = cpu_to_le32(0x80),
};
/*
* struct attr_def - Attribute definition entry ($AttrDef array)
*
* The data attribute of FILE_AttrDef contains a sequence of attribute
* definitions for the NTFS volume. With this, it is supposed to be safe for an
* older NTFS driver to mount a volume containing a newer NTFS version without
* damaging it (that's the theory. In practice it's: not damaging it too much).
* Entries are sorted by attribute type. The flags describe whether the
* attribute can be resident/non-resident and possibly other things, but the
* actual bits are unknown.
*
* @name: Unicode (UTF-16LE) name of the attribute (e.g. "$DATA", "$FILE_NAME").
* Zero-terminated string, maximum 0x40 characters (128 bytes).
* Used for human-readable display and debugging.
* @type: Attribute type code (ATTR_TYPE_* constants).
* Defines which attribute this entry describes.
* @display_rule: Default display rule (usually 0; rarely used in modern NTFS).
* Controls how the attribute is displayed in tools (legacy).
* @collation_rule: Default collation rule for indexing this attribute.
* Determines sort order when indexed (e.g. CASE_SENSITIVE, UNICODE).
* Used in $I30, $SDH, $SII, etc.
* @flags: Bit array of attribute constraints (ATTR_DEF_* flags).
* See ATTR_DEF_INDEXABLE, ATTR_DEF_MULTIPLE, etc.
* Defines whether the attribute can be indexed, multiple, resident-only, etc.
* @min_size: Optional minimum size of the attribute value (in bytes).
* 0 means no minimum enforced.
* @max_size: Maximum allowed size of the attribute value (in bytes).
*/
struct attr_def {
__le16 name[0x40];
__le32 type;
__le32 display_rule;
__le32 collation_rule;
__le32 flags;
__le64 min_size;
__le64 max_size;
} __packed;
static_assert(sizeof(struct attr_def) == 160);
/*
* enum - Attribute flags (16-bit) for non-resident attributes
*
* ATTR_IS_COMPRESSED: Attribute is compressed.
* If set, data is compressed using the method in
* ATTR_COMPRESSION_MASK.
* ATTR_COMPRESSION_MASK: Mask for compression method.
* Valid values are defined in NTFS compression types
* (e.g., 0x02 = LZNT1, etc.).
* Also serves as the first illegal value for method.
* ATTR_IS_ENCRYPTED: Attribute is encrypted.
* Data is encrypted using EFS (Encrypting File System).
* ATTR_IS_SPARSE: Attribute is sparse.
* Contains holes (unallocated regions) that read as zeros.
*/
enum {
ATTR_IS_COMPRESSED = cpu_to_le16(0x0001),
ATTR_COMPRESSION_MASK = cpu_to_le16(0x00ff),
ATTR_IS_ENCRYPTED = cpu_to_le16(0x4000),
ATTR_IS_SPARSE = cpu_to_le16(0x8000),
} __packed;
/*
* Attribute compression.
*
* Only the data attribute is ever compressed in the current ntfs driver in
* Windows. Further, compression is only applied when the data attribute is
* non-resident. Finally, to use compression, the maximum allowed cluster size
* on a volume is 4kib.
*
* The compression method is based on independently compressing blocks of X
* clusters, where X is determined from the compression_unit value found in the
* non-resident attribute record header (more precisely: X = 2^compression_unit
* clusters). On Windows NT/2k, X always is 16 clusters (compression_unit = 4).
*
* There are three different cases of how a compression block of X clusters
* can be stored:
*
* 1) The data in the block is all zero (a sparse block):
* This is stored as a sparse block in the runlist, i.e. the runlist
* entry has length = X and lcn = -1. The mapping pairs array actually
* uses a delta_lcn value length of 0, i.e. delta_lcn is not present at
* all, which is then interpreted by the driver as lcn = -1.
* NOTE: Even uncompressed files can be sparse on NTFS 3.0 volumes, then
* the same principles apply as above, except that the length is not
* restricted to being any particular value.
*
* 2) The data in the block is not compressed:
* This happens when compression doesn't reduce the size of the block
* in clusters. I.e. if compression has a small effect so that the
* compressed data still occupies X clusters, then the uncompressed data
* is stored in the block.
* This case is recognised by the fact that the runlist entry has
* length = X and lcn >= 0. The mapping pairs array stores this as
* normal with a run length of X and some specific delta_lcn, i.e.
* delta_lcn has to be present.
*
* 3) The data in the block is compressed:
* The common case. This case is recognised by the fact that the run
* list entry has length L < X and lcn >= 0. The mapping pairs array
* stores this as normal with a run length of X and some specific
* delta_lcn, i.e. delta_lcn has to be present. This runlist entry is
* immediately followed by a sparse entry with length = X - L and
* lcn = -1. The latter entry is to make up the vcn counting to the
* full compression block size X.
*
* In fact, life is more complicated because adjacent entries of the same type
* can be coalesced. This means that one has to keep track of the number of
* clusters handled and work on a basis of X clusters at a time being one
* block. An example: if length L > X this means that this particular runlist
* entry contains a block of length X and part of one or more blocks of length
* L - X. Another example: if length L < X, this does not necessarily mean that
* the block is compressed as it might be that the lcn changes inside the block
* and hence the following runlist entry describes the continuation of the
* potentially compressed block. The block would be compressed if the
* following runlist entry describes at least X - L sparse clusters, thus
* making up the compression block length as described in point 3 above. (Of
* course, there can be several runlist entries with small lengths so that the
* sparse entry does not follow the first data containing entry with
* length < X.)
*
* NOTE: At the end of the compressed attribute value, there most likely is not
* just the right amount of data to make up a compression block, thus this data
* is not even attempted to be compressed. It is just stored as is, unless
* the number of clusters it occupies is reduced when compressed in which case
* it is stored as a compressed compression block, complete with sparse
* clusters at the end.
*/
/*
* enum - Flags for resident attributes (8-bit)
*
* RESIDENT_ATTR_IS_INDEXED: Attribute is referenced in an index.
* (e.g., part of an index key or entry)
* Has implications for deletion and modification:
* - Cannot be freely removed if indexed
* - Index must be updated when value changes
* - Used for attributes like $FILE_NAME in directories
*/
enum {
RESIDENT_ATTR_IS_INDEXED = 0x01,
} __packed;
/*
* struct attr_record - NTFS attribute record header
*
* Common header for both resident and non-resident attributes.
* Always aligned to an 8-byte boundary on disk.
* Located at attrs_offset in the MFT record (see struct mft_record).
*
* @type: 32-bit attribute type (ATTR_TYPE_* constants).
* Identifies the attribute
* (e.g. 0x10 = $STANDARD_INFORMATION).
* @length: Total byte size of this attribute record (resident).
* 8-byte aligned; used to locate the next attribute.
* @non_resident: 0 = resident attribute
* 1 = non-resident attribute
* @name_length: Number of Unicode characters in the attribute name.
* 0 if unnamed (most system attributes are unnamed).
* @name_offset: Byte offset from start of attribute record to the name.
* 8-byte aligned; when creating, place at end of header.
* @flags: Attribute flags (see ATTR_IS_COMPRESSED,
* ATTR_IS_ENCRYPTED, etc.).
* For resident: see RESIDENT_ATTR_* flags.
* @instance: Unique instance number within this MFT record.
* Incremented via next_attr_instance; unique per record.
*
* Resident attributes (when @non_resident == 0):
* @data.resident.value_length: Byte size of the attribute value.
* @data.resident.value_offset: Byte offset from start of attribute
* record to the value data.
* 8-byte aligned if name present.
* @data.resident.flags: Resident-specific flags
* @data.resident.reserved: Reserved/alignment to 8 bytes.
*
* Non-resident attributes (when @non_resident == 1):
* @data.non_resident.lowest_vcn: Lowest valid VCN in this extent.
* Usually 0 unless attribute list is used.
* @data.non_resident.highest_vcn: Highest valid VCN in this extent.
* -1 for zero-length, 0 for single extent.
* @data.non_resident.mapping_pairs_offset:
* Byte offset to mapping pairs array
* (VCN → LCN mappings).
* 8-byte aligned when creating.
* @data.non_resident.compression_unit:
* Log2 of clusters per compression unit.
* 0 = not compressed.
* WinNT4 used 4; sparse files use 0
* on XP SP2+.
* @data.non_resident.reserved: 5 bytes for 8-byte alignment.
* @data.non_resident.allocated_size:
* Allocated disk space in bytes.
* For compressed: logical allocated size.
* @data.non_resident.data_size: Logical attribute value size in bytes.
* Can be larger than allocated_size if
* compressed/sparse.
* @data.non_resident.initialized_size:
* Initialized portion size in bytes.
* Usually equals data_size.
* @data.non_resident.compressed_size:
* Compressed on-disk size in bytes.
* Only present when compressed or sparse.
* Actual disk usage.
*/
struct attr_record {
__le32 type;
__le32 length;
u8 non_resident;
u8 name_length;
__le16 name_offset;
__le16 flags;
__le16 instance;
union {
struct {
__le32 value_length;
__le16 value_offset;
u8 flags;
s8 reserved;
} __packed resident;
struct {
__le64 lowest_vcn;
__le64 highest_vcn;
__le16 mapping_pairs_offset;
u8 compression_unit;
u8 reserved[5];
__le64 allocated_size;
__le64 data_size;
__le64 initialized_size;
__le64 compressed_size;
} __packed non_resident;
} __packed data;
} __packed;
/*
* enum - NTFS file attribute flags (32-bit)
*
* File attribute flags (32-bit) appearing in the file_attributes fields of the
* STANDARD_INFORMATION attribute of MFT_RECORDs and the FILENAME_ATTR
* attributes of MFT_RECORDs and directory index entries.
*
* All of the below flags appear in the directory index entries but only some
* appear in the STANDARD_INFORMATION attribute whilst only some others appear
* in the FILENAME_ATTR attribute of MFT_RECORDs. Unless otherwise stated the
* flags appear in all of the above.
*
* FILE_ATTR_READONLY: File is read-only.
* FILE_ATTR_HIDDEN: File is hidden (not shown by default).
* FILE_ATTR_SYSTEM: System file (protected by OS).
* FILE_ATTR_DIRECTORY: Directory flag (reserved in NT; use MFT flag instead).
* FILE_ATTR_ARCHIVE: File needs archiving (backup flag).
* FILE_ATTR_DEVICE: Device file (rarely used).
* FILE_ATTR_NORMAL: Normal file (no special attributes).
* FILE_ATTR_TEMPORARY: Temporary file (delete on close).
* FILE_ATTR_SPARSE_FILE: Sparse file (contains holes).
* FILE_ATTR_REPARSE_POINT: Reparse point (junction, symlink, mount point).
* FILE_ATTR_COMPRESSED: File is compressed.
* FILE_ATTR_OFFLINE: File data is offline (not locally available).
* FILE_ATTR_NOT_CONTENT_INDEXED:
* File is excluded from content indexing.
* FILE_ATTR_ENCRYPTED: File is encrypted (EFS).
* FILE_ATTR_VALID_FLAGS: Mask of all valid flags for reading.
* FILE_ATTR_VALID_SET_FLAGS: Mask of flags that can be set by user.
* FILE_ATTRIBUTE_RECALL_ON_OPEN:
* Recall data on open (cloud/HSM related).
* FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT:
* $FILE_NAME has duplicate index entry.
* FILE_ATTR_DUP_VIEW_INDEX_PRESENT:
* Duplicate view index present (object ID, quota, etc.).
*/
enum {
FILE_ATTR_READONLY = cpu_to_le32(0x00000001),
FILE_ATTR_HIDDEN = cpu_to_le32(0x00000002),
FILE_ATTR_SYSTEM = cpu_to_le32(0x00000004),
/* Old DOS volid. Unused in NT. = cpu_to_le32(0x00000008), */
FILE_ATTR_DIRECTORY = cpu_to_le32(0x00000010),
FILE_ATTR_ARCHIVE = cpu_to_le32(0x00000020),
FILE_ATTR_DEVICE = cpu_to_le32(0x00000040),
FILE_ATTR_NORMAL = cpu_to_le32(0x00000080),
FILE_ATTR_TEMPORARY = cpu_to_le32(0x00000100),
FILE_ATTR_SPARSE_FILE = cpu_to_le32(0x00000200),
FILE_ATTR_REPARSE_POINT = cpu_to_le32(0x00000400),
FILE_ATTR_COMPRESSED = cpu_to_le32(0x00000800),
FILE_ATTR_OFFLINE = cpu_to_le32(0x00001000),
FILE_ATTR_NOT_CONTENT_INDEXED = cpu_to_le32(0x00002000),
FILE_ATTR_ENCRYPTED = cpu_to_le32(0x00004000),
FILE_ATTR_VALID_FLAGS = cpu_to_le32(0x00007fb7),
FILE_ATTR_VALID_SET_FLAGS = cpu_to_le32(0x000031a7),
FILE_ATTRIBUTE_RECALL_ON_OPEN = cpu_to_le32(0x00040000),
FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT = cpu_to_le32(0x10000000),
FILE_ATTR_DUP_VIEW_INDEX_PRESENT = cpu_to_le32(0x20000000),
};
/*
* NOTE on times in NTFS: All times are in MS standard time format, i.e. they
* are the number of 100-nanosecond intervals since 1st January 1601, 00:00:00
* universal coordinated time (UTC). (In Linux time starts 1st January 1970,
* 00:00:00 UTC and is stored as the number of 1-second intervals since then.)
*/
/*
* struct standard_information - $STANDARD_INFORMATION attribute content
*
* NOTE: Always resident.
* NOTE: Present in all base file records on a volume.
* NOTE: There is conflicting information about the meaning of each of the time
* fields but the meaning as defined below has been verified to be
* correct by practical experimentation on Windows NT4 SP6a and is hence
* assumed to be the one and only correct interpretation.
*
* @creation_time: File creation time (NTFS timestamp).
* Updated on filename change(?).
* @last_data_change_time: Last modification time of data streams.
* @last_mft_change_time: Last modification time of this MFT record.
* @last_access_time: Last access time (approximate).
* Not updated on read-only volumes; can be disabled.
* @file_attributes: File attribute flags (FILE_ATTR_* bits).
*
* Union (version-specific fields):
* @ver.v1.reserved12: 12 bytes reserved/alignment (NTFS 1.2 only).
*
* @ver.v3 (NTFS 3.x / Windows 2000+):
* @maximum_versions: Max allowed file versions (0 = disabled).
* @version_number: Current version number (0 if disabled).
* @class_id: Class ID (from bidirectional index?).
* @owner_id: Owner ID (maps to $Quota via $Q index).
* @security_id: Security ID (maps to $Secure $SII/$SDS).
* @quota_charged: Quota charge in bytes (0 if quotas disabled).
* @usn: Last USN from $UsnJrnl (0 if disabled).
*/
struct standard_information {
__le64 creation_time;
__le64 last_data_change_time;
__le64 last_mft_change_time;
__le64 last_access_time;
__le32 file_attributes;
union {
struct {
u8 reserved12[12];
} __packed v1;
struct {
__le32 maximum_versions;
__le32 version_number;
__le32 class_id;
__le32 owner_id;
__le32 security_id;
__le64 quota_charged;
__le64 usn;
} __packed v3;
} __packed ver;
} __packed;
/*
* struct attr_list_entry - Entry in $ATTRIBUTE_LIST attribute.
*
* @type: Attribute type code (ATTR_TYPE_*).
* @length: Byte size of this entry (8-byte aligned).
* @name_length: Unicode char count of attribute name (0 if unnamed).
* @name_offset: Byte offset from start of entry to name (always set).
* @lowest_vcn: Lowest VCN of this attribute extent (usually 0).
* Signed value; non-zero when attribute spans extents.
* @mft_reference: MFT record reference holding this attribute extent.
* @instance: Attribute instance number (if lowest_vcn == 0); else 0.
* @name: Variable Unicode name (use @name_offset when reading).
*
* - Can be either resident or non-resident.
* - Value consists of a sequence of variable length, 8-byte aligned,
* ATTR_LIST_ENTRY records.
* - The list is not terminated by anything at all! The only way to know when
* the end is reached is to keep track of the current offset and compare it to
* the attribute value size.
* - The attribute list attribute contains one entry for each attribute of
* the file in which the list is located, except for the list attribute
* itself. The list is sorted: first by attribute type, second by attribute
* name (if present), third by instance number. The extents of one
* non-resident attribute (if present) immediately follow after the initial