1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * usnjrnl.h - Defines for NTFS kernel transaction log ($UsnJrnl) handling. 4*4882a593Smuzhiyun * Part of the Linux-NTFS project. 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * Copyright (c) 2005 Anton Altaparmakov 7*4882a593Smuzhiyun */ 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun #ifndef _LINUX_NTFS_USNJRNL_H 10*4882a593Smuzhiyun #define _LINUX_NTFS_USNJRNL_H 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun #ifdef NTFS_RW 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun #include "types.h" 15*4882a593Smuzhiyun #include "endian.h" 16*4882a593Smuzhiyun #include "layout.h" 17*4882a593Smuzhiyun #include "volume.h" 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun /* 20*4882a593Smuzhiyun * Transaction log ($UsnJrnl) organization: 21*4882a593Smuzhiyun * 22*4882a593Smuzhiyun * The transaction log records whenever a file is modified in any way. So for 23*4882a593Smuzhiyun * example it will record that file "blah" was written to at a particular time 24*4882a593Smuzhiyun * but not what was written. If will record that a file was deleted or 25*4882a593Smuzhiyun * created, that a file was truncated, etc. See below for all the reason 26*4882a593Smuzhiyun * codes used. 27*4882a593Smuzhiyun * 28*4882a593Smuzhiyun * The transaction log is in the $Extend directory which is in the root 29*4882a593Smuzhiyun * directory of each volume. If it is not present it means transaction 30*4882a593Smuzhiyun * logging is disabled. If it is present it means transaction logging is 31*4882a593Smuzhiyun * either enabled or in the process of being disabled in which case we can 32*4882a593Smuzhiyun * ignore it as it will go away as soon as Windows gets its hands on it. 33*4882a593Smuzhiyun * 34*4882a593Smuzhiyun * To determine whether the transaction logging is enabled or in the process 35*4882a593Smuzhiyun * of being disabled, need to check the volume flags in the 36*4882a593Smuzhiyun * $VOLUME_INFORMATION attribute in the $Volume system file (which is present 37*4882a593Smuzhiyun * in the root directory and has a fixed mft record number, see layout.h). 38*4882a593Smuzhiyun * If the flag VOLUME_DELETE_USN_UNDERWAY is set it means the transaction log 39*4882a593Smuzhiyun * is in the process of being disabled and if this flag is clear it means the 40*4882a593Smuzhiyun * transaction log is enabled. 41*4882a593Smuzhiyun * 42*4882a593Smuzhiyun * The transaction log consists of two parts; the $DATA/$Max attribute as well 43*4882a593Smuzhiyun * as the $DATA/$J attribute. $Max is a header describing the transaction 44*4882a593Smuzhiyun * log whilst $J is the transaction log data itself as a sequence of variable 45*4882a593Smuzhiyun * sized USN_RECORDs (see below for all the structures). 46*4882a593Smuzhiyun * 47*4882a593Smuzhiyun * We do not care about transaction logging at this point in time but we still 48*4882a593Smuzhiyun * need to let windows know that the transaction log is out of date. To do 49*4882a593Smuzhiyun * this we need to stamp the transaction log. This involves setting the 50*4882a593Smuzhiyun * lowest_valid_usn field in the $DATA/$Max attribute to the usn to be used 51*4882a593Smuzhiyun * for the next added USN_RECORD to the $DATA/$J attribute as well as 52*4882a593Smuzhiyun * generating a new journal_id in $DATA/$Max. 53*4882a593Smuzhiyun * 54*4882a593Smuzhiyun * The journal_id is as of the current version (2.0) of the transaction log 55*4882a593Smuzhiyun * simply the 64-bit timestamp of when the journal was either created or last 56*4882a593Smuzhiyun * stamped. 57*4882a593Smuzhiyun * 58*4882a593Smuzhiyun * To determine the next usn there are two ways. The first is to parse 59*4882a593Smuzhiyun * $DATA/$J and to find the last USN_RECORD in it and to add its record_length 60*4882a593Smuzhiyun * to its usn (which is the byte offset in the $DATA/$J attribute). The 61*4882a593Smuzhiyun * second is simply to take the data size of the attribute. Since the usns 62*4882a593Smuzhiyun * are simply byte offsets into $DATA/$J, this is exactly the next usn. For 63*4882a593Smuzhiyun * obvious reasons we use the second method as it is much simpler and faster. 64*4882a593Smuzhiyun * 65*4882a593Smuzhiyun * As an aside, note that to actually disable the transaction log, one would 66*4882a593Smuzhiyun * need to set the VOLUME_DELETE_USN_UNDERWAY flag (see above), then go 67*4882a593Smuzhiyun * through all the mft records on the volume and set the usn field in their 68*4882a593Smuzhiyun * $STANDARD_INFORMATION attribute to zero. Once that is done, one would need 69*4882a593Smuzhiyun * to delete the transaction log file, i.e. \$Extent\$UsnJrnl, and finally, 70*4882a593Smuzhiyun * one would need to clear the VOLUME_DELETE_USN_UNDERWAY flag. 71*4882a593Smuzhiyun * 72*4882a593Smuzhiyun * Note that if a volume is unmounted whilst the transaction log is being 73*4882a593Smuzhiyun * disabled, the process will continue the next time the volume is mounted. 74*4882a593Smuzhiyun * This is why we can safely mount read-write when we see a transaction log 75*4882a593Smuzhiyun * in the process of being deleted. 76*4882a593Smuzhiyun */ 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun /* Some $UsnJrnl related constants. */ 79*4882a593Smuzhiyun #define UsnJrnlMajorVer 2 80*4882a593Smuzhiyun #define UsnJrnlMinorVer 0 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun /* 83*4882a593Smuzhiyun * $DATA/$Max attribute. This is (always?) resident and has a fixed size of 84*4882a593Smuzhiyun * 32 bytes. It contains the header describing the transaction log. 85*4882a593Smuzhiyun */ 86*4882a593Smuzhiyun typedef struct { 87*4882a593Smuzhiyun /*Ofs*/ 88*4882a593Smuzhiyun /* 0*/sle64 maximum_size; /* The maximum on-disk size of the $DATA/$J 89*4882a593Smuzhiyun attribute. */ 90*4882a593Smuzhiyun /* 8*/sle64 allocation_delta; /* Number of bytes by which to increase the 91*4882a593Smuzhiyun size of the $DATA/$J attribute. */ 92*4882a593Smuzhiyun /*0x10*/sle64 journal_id; /* Current id of the transaction log. */ 93*4882a593Smuzhiyun /*0x18*/leUSN lowest_valid_usn; /* Lowest valid usn in $DATA/$J for the 94*4882a593Smuzhiyun current journal_id. */ 95*4882a593Smuzhiyun /* sizeof() = 32 (0x20) bytes */ 96*4882a593Smuzhiyun } __attribute__ ((__packed__)) USN_HEADER; 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun /* 99*4882a593Smuzhiyun * Reason flags (32-bit). Cumulative flags describing the change(s) to the 100*4882a593Smuzhiyun * file since it was last opened. I think the names speak for themselves but 101*4882a593Smuzhiyun * if you disagree check out the descriptions in the Linux NTFS project NTFS 102*4882a593Smuzhiyun * documentation: http://www.linux-ntfs.org/ 103*4882a593Smuzhiyun */ 104*4882a593Smuzhiyun enum { 105*4882a593Smuzhiyun USN_REASON_DATA_OVERWRITE = cpu_to_le32(0x00000001), 106*4882a593Smuzhiyun USN_REASON_DATA_EXTEND = cpu_to_le32(0x00000002), 107*4882a593Smuzhiyun USN_REASON_DATA_TRUNCATION = cpu_to_le32(0x00000004), 108*4882a593Smuzhiyun USN_REASON_NAMED_DATA_OVERWRITE = cpu_to_le32(0x00000010), 109*4882a593Smuzhiyun USN_REASON_NAMED_DATA_EXTEND = cpu_to_le32(0x00000020), 110*4882a593Smuzhiyun USN_REASON_NAMED_DATA_TRUNCATION= cpu_to_le32(0x00000040), 111*4882a593Smuzhiyun USN_REASON_FILE_CREATE = cpu_to_le32(0x00000100), 112*4882a593Smuzhiyun USN_REASON_FILE_DELETE = cpu_to_le32(0x00000200), 113*4882a593Smuzhiyun USN_REASON_EA_CHANGE = cpu_to_le32(0x00000400), 114*4882a593Smuzhiyun USN_REASON_SECURITY_CHANGE = cpu_to_le32(0x00000800), 115*4882a593Smuzhiyun USN_REASON_RENAME_OLD_NAME = cpu_to_le32(0x00001000), 116*4882a593Smuzhiyun USN_REASON_RENAME_NEW_NAME = cpu_to_le32(0x00002000), 117*4882a593Smuzhiyun USN_REASON_INDEXABLE_CHANGE = cpu_to_le32(0x00004000), 118*4882a593Smuzhiyun USN_REASON_BASIC_INFO_CHANGE = cpu_to_le32(0x00008000), 119*4882a593Smuzhiyun USN_REASON_HARD_LINK_CHANGE = cpu_to_le32(0x00010000), 120*4882a593Smuzhiyun USN_REASON_COMPRESSION_CHANGE = cpu_to_le32(0x00020000), 121*4882a593Smuzhiyun USN_REASON_ENCRYPTION_CHANGE = cpu_to_le32(0x00040000), 122*4882a593Smuzhiyun USN_REASON_OBJECT_ID_CHANGE = cpu_to_le32(0x00080000), 123*4882a593Smuzhiyun USN_REASON_REPARSE_POINT_CHANGE = cpu_to_le32(0x00100000), 124*4882a593Smuzhiyun USN_REASON_STREAM_CHANGE = cpu_to_le32(0x00200000), 125*4882a593Smuzhiyun USN_REASON_CLOSE = cpu_to_le32(0x80000000), 126*4882a593Smuzhiyun }; 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun typedef le32 USN_REASON_FLAGS; 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun /* 131*4882a593Smuzhiyun * Source info flags (32-bit). Information about the source of the change(s) 132*4882a593Smuzhiyun * to the file. For detailed descriptions of what these mean, see the Linux 133*4882a593Smuzhiyun * NTFS project NTFS documentation: 134*4882a593Smuzhiyun * http://www.linux-ntfs.org/ 135*4882a593Smuzhiyun */ 136*4882a593Smuzhiyun enum { 137*4882a593Smuzhiyun USN_SOURCE_DATA_MANAGEMENT = cpu_to_le32(0x00000001), 138*4882a593Smuzhiyun USN_SOURCE_AUXILIARY_DATA = cpu_to_le32(0x00000002), 139*4882a593Smuzhiyun USN_SOURCE_REPLICATION_MANAGEMENT = cpu_to_le32(0x00000004), 140*4882a593Smuzhiyun }; 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun typedef le32 USN_SOURCE_INFO_FLAGS; 143*4882a593Smuzhiyun 144*4882a593Smuzhiyun /* 145*4882a593Smuzhiyun * $DATA/$J attribute. This is always non-resident, is marked as sparse, and 146*4882a593Smuzhiyun * is of variabled size. It consists of a sequence of variable size 147*4882a593Smuzhiyun * USN_RECORDS. The minimum allocated_size is allocation_delta as 148*4882a593Smuzhiyun * specified in $DATA/$Max. When the maximum_size specified in $DATA/$Max is 149*4882a593Smuzhiyun * exceeded by more than allocation_delta bytes, allocation_delta bytes are 150*4882a593Smuzhiyun * allocated and appended to the $DATA/$J attribute and an equal number of 151*4882a593Smuzhiyun * bytes at the beginning of the attribute are freed and made sparse. Note the 152*4882a593Smuzhiyun * making sparse only happens at volume checkpoints and hence the actual 153*4882a593Smuzhiyun * $DATA/$J size can exceed maximum_size + allocation_delta temporarily. 154*4882a593Smuzhiyun */ 155*4882a593Smuzhiyun typedef struct { 156*4882a593Smuzhiyun /*Ofs*/ 157*4882a593Smuzhiyun /* 0*/le32 length; /* Byte size of this record (8-byte 158*4882a593Smuzhiyun aligned). */ 159*4882a593Smuzhiyun /* 4*/le16 major_ver; /* Major version of the transaction log used 160*4882a593Smuzhiyun for this record. */ 161*4882a593Smuzhiyun /* 6*/le16 minor_ver; /* Minor version of the transaction log used 162*4882a593Smuzhiyun for this record. */ 163*4882a593Smuzhiyun /* 8*/leMFT_REF mft_reference;/* The mft reference of the file (or 164*4882a593Smuzhiyun directory) described by this record. */ 165*4882a593Smuzhiyun /*0x10*/leMFT_REF parent_directory;/* The mft reference of the parent 166*4882a593Smuzhiyun directory of the file described by this 167*4882a593Smuzhiyun record. */ 168*4882a593Smuzhiyun /*0x18*/leUSN usn; /* The usn of this record. Equals the offset 169*4882a593Smuzhiyun within the $DATA/$J attribute. */ 170*4882a593Smuzhiyun /*0x20*/sle64 time; /* Time when this record was created. */ 171*4882a593Smuzhiyun /*0x28*/USN_REASON_FLAGS reason;/* Reason flags (see above). */ 172*4882a593Smuzhiyun /*0x2c*/USN_SOURCE_INFO_FLAGS source_info;/* Source info flags (see above). */ 173*4882a593Smuzhiyun /*0x30*/le32 security_id; /* File security_id copied from 174*4882a593Smuzhiyun $STANDARD_INFORMATION. */ 175*4882a593Smuzhiyun /*0x34*/FILE_ATTR_FLAGS file_attributes; /* File attributes copied from 176*4882a593Smuzhiyun $STANDARD_INFORMATION or $FILE_NAME (not 177*4882a593Smuzhiyun sure which). */ 178*4882a593Smuzhiyun /*0x38*/le16 file_name_size; /* Size of the file name in bytes. */ 179*4882a593Smuzhiyun /*0x3a*/le16 file_name_offset; /* Offset to the file name in bytes from the 180*4882a593Smuzhiyun start of this record. */ 181*4882a593Smuzhiyun /*0x3c*/ntfschar file_name[0]; /* Use when creating only. When reading use 182*4882a593Smuzhiyun file_name_offset to determine the location 183*4882a593Smuzhiyun of the name. */ 184*4882a593Smuzhiyun /* sizeof() = 60 (0x3c) bytes */ 185*4882a593Smuzhiyun } __attribute__ ((__packed__)) USN_RECORD; 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun extern bool ntfs_stamp_usnjrnl(ntfs_volume *vol); 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun #endif /* NTFS_RW */ 190*4882a593Smuzhiyun 191*4882a593Smuzhiyun #endif /* _LINUX_NTFS_USNJRNL_H */ 192