usnjrnl.h 8.4 KB

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