xref: /linux/fs/ntfs/logfile.h (revision cdd4dc3aebeab43a72ce0bc2b5bab6f0a80b97a5)
11e9ea7e0SNamjae Jeon /* SPDX-License-Identifier: GPL-2.0-or-later */
21e9ea7e0SNamjae Jeon /*
3*40796051SNamjae Jeon  * Defines for NTFS kernel journal (LogFile) handling.
41e9ea7e0SNamjae Jeon  *
51e9ea7e0SNamjae Jeon  * Copyright (c) 2000-2005 Anton Altaparmakov
61e9ea7e0SNamjae Jeon  */
71e9ea7e0SNamjae Jeon 
81e9ea7e0SNamjae Jeon #ifndef _LINUX_NTFS_LOGFILE_H
91e9ea7e0SNamjae Jeon #define _LINUX_NTFS_LOGFILE_H
101e9ea7e0SNamjae Jeon 
111e9ea7e0SNamjae Jeon #include "layout.h"
121e9ea7e0SNamjae Jeon 
131e9ea7e0SNamjae Jeon /*
14*40796051SNamjae Jeon  * Journal (LogFile) organization:
151e9ea7e0SNamjae Jeon  *
161e9ea7e0SNamjae Jeon  * Two restart areas present in the first two pages (restart pages, one restart
171e9ea7e0SNamjae Jeon  * area in each page).  When the volume is dismounted they should be identical,
181e9ea7e0SNamjae Jeon  * except for the update sequence array which usually has a different update
191e9ea7e0SNamjae Jeon  * sequence number.
201e9ea7e0SNamjae Jeon  *
211e9ea7e0SNamjae Jeon  * These are followed by log records organized in pages headed by a log record
221e9ea7e0SNamjae Jeon  * header going up to log file size.  Not all pages contain log records when a
231e9ea7e0SNamjae Jeon  * volume is first formatted, but as the volume ages, all records will be used.
241e9ea7e0SNamjae Jeon  * When the log file fills up, the records at the beginning are purged (by
251e9ea7e0SNamjae Jeon  * modifying the oldest_lsn to a higher value presumably) and writing begins
261e9ea7e0SNamjae Jeon  * at the beginning of the file.  Effectively, the log file is viewed as a
271e9ea7e0SNamjae Jeon  * circular entity.
281e9ea7e0SNamjae Jeon  *
291e9ea7e0SNamjae Jeon  * NOTE: Windows NT, 2000, and XP all use log file version 1.1 but they accept
301e9ea7e0SNamjae Jeon  * versions <= 1.x, including 0.-1.  (Yes, that is a minus one in there!)  We
311e9ea7e0SNamjae Jeon  * probably only want to support 1.1 as this seems to be the current version
321e9ea7e0SNamjae Jeon  * and we don't know how that differs from the older versions.  The only
331e9ea7e0SNamjae Jeon  * exception is if the journal is clean as marked by the two restart pages
341e9ea7e0SNamjae Jeon  * then it doesn't matter whether we are on an earlier version.  We can just
351e9ea7e0SNamjae Jeon  * reinitialize the logfile and start again with version 1.1.
361e9ea7e0SNamjae Jeon  */
371e9ea7e0SNamjae Jeon 
38*40796051SNamjae Jeon /* Some LogFile related constants. */
391e9ea7e0SNamjae Jeon #define MaxLogFileSize		0x100000000ULL
401e9ea7e0SNamjae Jeon #define DefaultLogPageSize	4096
411e9ea7e0SNamjae Jeon #define MinLogRecordPages	48
421e9ea7e0SNamjae Jeon 
431e9ea7e0SNamjae Jeon /*
441e9ea7e0SNamjae Jeon  * Log file restart page header (begins the restart area).
45*40796051SNamjae Jeon  *
46*40796051SNamjae Jeon  * @magic: The magic is "RSTR".
47*40796051SNamjae Jeon  * @usa_ofs: See ntfs_record struct definition in layout.h.  When creating,
48*40796051SNamjae Jeon  *   set this to be immediately after this header structure (without any
49*40796051SNamjae Jeon  *   alignment).
50*40796051SNamjae Jeon  * @usa_count: See ntfs_record struct definition in layout.h.
51*40796051SNamjae Jeon  * @chkdsk_lsn: The last log file sequence number found by chkdsk.  Only
52*40796051SNamjae Jeon  *   used when the magic is changed to "CHKD".  Otherwise this is zero.
53*40796051SNamjae Jeon  * @system_page_size: Byte size of system pages when the log file was
54*40796051SNamjae Jeon  *   created, has to be >= 512 and a power of 2.  Use this to calculate
55*40796051SNamjae Jeon  *   the required size of the usa (usa_count) and add it to usa_ofs. Then
56*40796051SNamjae Jeon  *   verify that the result is less than the value of
57*40796051SNamjae Jeon  *   the restart_area_offset.
58*40796051SNamjae Jeon  * @log_page_size: Byte size of log file pages, has to be >= 512 and
59*40796051SNamjae Jeon  *   a power of 2.  The default is 4096 and is used when the system page
60*40796051SNamjae Jeon  *   size is between 4096 and 8192.  Otherwise this is set to the system
61*40796051SNamjae Jeon  *   page size instead.
62*40796051SNamjae Jeon  * @restart_area_offset: Byte offset from the start of this header to
63*40796051SNamjae Jeon  *   the RESTART_AREA. Value has to be aligned to 8-byte boundary.  When
64*40796051SNamjae Jeon  *   creating, set this to be after the usa.
65*40796051SNamjae Jeon  * @minor_ver: Log file minor version.  Only check if major version is 1.
66*40796051SNamjae Jeon  * @major_ver: Log file major version.  We only support version 1.1.
671e9ea7e0SNamjae Jeon  */
68*40796051SNamjae Jeon struct restart_page_header {
69*40796051SNamjae Jeon 	__le32 magic;
70*40796051SNamjae Jeon 	__le16 usa_ofs;
71*40796051SNamjae Jeon 	__le16 usa_count;
72*40796051SNamjae Jeon 	__le64 chkdsk_lsn;
73*40796051SNamjae Jeon 	__le32 system_page_size;
74*40796051SNamjae Jeon 	__le32 log_page_size;
75*40796051SNamjae Jeon 	__le16 restart_area_offset;
76*40796051SNamjae Jeon 	__le16 minor_ver;
77*40796051SNamjae Jeon 	__le16 major_ver;
78*40796051SNamjae Jeon } __packed;
791e9ea7e0SNamjae Jeon 
801e9ea7e0SNamjae Jeon /*
811e9ea7e0SNamjae Jeon  * Constant for the log client indices meaning that there are no client records
821e9ea7e0SNamjae Jeon  * in this particular client array.  Also inside the client records themselves,
831e9ea7e0SNamjae Jeon  * this means that there are no client records preceding or following this one.
841e9ea7e0SNamjae Jeon  */
851e9ea7e0SNamjae Jeon #define LOGFILE_NO_CLIENT	cpu_to_le16(0xffff)
861e9ea7e0SNamjae Jeon #define LOGFILE_NO_CLIENT_CPU	0xffff
871e9ea7e0SNamjae Jeon 
881e9ea7e0SNamjae Jeon /*
891e9ea7e0SNamjae Jeon  * These are the so far known RESTART_AREA_* flags (16-bit) which contain
901e9ea7e0SNamjae Jeon  * information about the log file in which they are present.
91*40796051SNamjae Jeon  * gcc: Force enum bit width to 16.
921e9ea7e0SNamjae Jeon  */
931e9ea7e0SNamjae Jeon enum {
941e9ea7e0SNamjae Jeon 	RESTART_VOLUME_IS_CLEAN	= cpu_to_le16(0x0002),
95*40796051SNamjae Jeon 	RESTART_SPACE_FILLER	= cpu_to_le16(0xffff),
96*40796051SNamjae Jeon } __packed;
971e9ea7e0SNamjae Jeon 
981e9ea7e0SNamjae Jeon /*
991e9ea7e0SNamjae Jeon  * Log file restart area record.  The offset of this record is found by adding
1001e9ea7e0SNamjae Jeon  * the offset of the RESTART_PAGE_HEADER to the restart_area_offset value found
1011e9ea7e0SNamjae Jeon  * in it.  See notes at restart_area_offset above.
102*40796051SNamjae Jeon  *
103*40796051SNamjae Jeon  * @current_lsn: The current, i.e. last LSN inside the log when
104*40796051SNamjae Jeon  *   the restart area was last written.  This happens often but what is
105*40796051SNamjae Jeon  *   the interval?  Is it just fixed time or is it every time a check point
106*40796051SNamjae Jeon  *   is written or somethine else?  On create set to 0.
107*40796051SNamjae Jeon  * @log_clients: Number of log client records in the array of log client
108*40796051SNamjae Jeon  *   records which follows this restart area.  Must be 1.
109*40796051SNamjae Jeon  * @client_free_list: The index of the first free log client record in
110*40796051SNamjae Jeon  *   the array of log client records.  LOGFILE_NO_CLIENT means that there
111*40796051SNamjae Jeon  *   are no free log client records in the array.  If != LOGFILE_NO_CLIENT,
112*40796051SNamjae Jeon  *   check that log_clients > client_free_list.  On Win2k and presumably
113*40796051SNamjae Jeon  *   earlier, on a clean volume this is != LOGFILE_NO_CLIENT, and it should
114*40796051SNamjae Jeon  *   be 0, i.e. the first (and only) client record is free and thus
115*40796051SNamjae Jeon  *   the logfile is closed and hence clean.  A dirty volume would have left
116*40796051SNamjae Jeon  *   the logfile open and hence this would be LOGFILE_NO_CLIENT.  On WinXP
117*40796051SNamjae Jeon  *   and presumably later, the logfile is always open, even on clean
118*40796051SNamjae Jeon  *   shutdown so this should always be LOGFILE_NO_CLIENT.
119*40796051SNamjae Jeon  * @client_in_use_list: The index of the first in-use log client record in
120*40796051SNamjae Jeon  *   the array of log client records.  LOGFILE_NO_CLIENT means that there
121*40796051SNamjae Jeon  *   are no in-use log client records in the array.
122*40796051SNamjae Jeon  *   If != LOGFILE_NO_CLIENT check that log_clients > client_in_use_list.
123*40796051SNamjae Jeon  *   On Win2k and presumably earlier, on a clean volume this is
124*40796051SNamjae Jeon  *   LOGFILE_NO_CLIENT, i.e. there are no client records in use and thus
125*40796051SNamjae Jeon  *   the logfile is closed and hence clean.  A dirty volume would have left
126*40796051SNamjae Jeon  *   the logfile open and hence this would be != LOGFILE_NO_CLIENT, and it
127*40796051SNamjae Jeon  *   should be 0, i.e. the first (and only) client record is in use.  On
128*40796051SNamjae Jeon  *   WinXP and presumably later, the logfile is always open, even on clean
129*40796051SNamjae Jeon  *   shutdown so this should always be 0.
130*40796051SNamjae Jeon  * @flags: Flags modifying LFS behaviour.  On Win2k and presumably earlier
131*40796051SNamjae Jeon  *   this is always 0.  On WinXP and presumably later, if the logfile was
132*40796051SNamjae Jeon  *   shutdown cleanly, the second bit, RESTART_VOLUME_IS_CLEAN, is set.
133*40796051SNamjae Jeon  *   This bit is cleared when the volume is mounted by WinXP and set when
134*40796051SNamjae Jeon  *   the volume is dismounted, thus if the logfile is dirty, this bit is
135*40796051SNamjae Jeon  *   clear.  Thus we don't need to check the Windows version to determine
136*40796051SNamjae Jeon  *   if the logfile is clean.  Instead if the logfile is closed, we know
137*40796051SNamjae Jeon  *   it must be clean.  If it is open and this bit is set, we also know
138*40796051SNamjae Jeon  *   it must be clean.  If on the other hand the logfile is open and this
139*40796051SNamjae Jeon  *   bit is clear, we can be almost certain that the logfile is dirty.
140*40796051SNamjae Jeon  * @seq_number_bits: How many bits to use for the sequence number.  This
141*40796051SNamjae Jeon  *   is calculated as 67 - the number of bits required to store the logfile
142*40796051SNamjae Jeon  *   size in bytes and this can be used in with the specified file_size as
143*40796051SNamjae Jeon  *   a consistency check.
144*40796051SNamjae Jeon  * @restart_area_length: Length of the restart area including the client
145*40796051SNamjae Jeon  *   array.  Following checks required if version matches.  Otherwise,
146*40796051SNamjae Jeon  *   skip them.  restart_area_offset + restart_area_length has to be
147*40796051SNamjae Jeon  *   <= system_page_size.  Also, restart_area_length has to be >=
148*40796051SNamjae Jeon  *   client_array_offset + (log_clients * sizeof(log client record)).
149*40796051SNamjae Jeon  * @client_array_offset: Offset from the start of this record to the first
150*40796051SNamjae Jeon  *   log client record if versions are matched.  When creating, set this
151*40796051SNamjae Jeon  *   to be after this restart area structure, aligned to 8-bytes boundary.
152*40796051SNamjae Jeon  *   If the versions do not match, this is ignored and the offset is
153*40796051SNamjae Jeon  *   assumed to be (sizeof(RESTART_AREA) + 7) &  ~7, i.e. rounded up to
154*40796051SNamjae Jeon  *   first 8-byte boundary.  Either way, client_array_offset has to be
155*40796051SNamjae Jeon  *   aligned to an 8-byte boundary.  Also, restart_area_offset +
156*40796051SNamjae Jeon  *   client_array_offset has to be <= 510.  Finally, client_array_offset +
157*40796051SNamjae Jeon  *   (log_clients * sizeof(log client record)) has to be <= system_page_size.
158*40796051SNamjae Jeon  *   On Win2k and presumably earlier, this is 0x30, i.e. immediately
159*40796051SNamjae Jeon  *   following this record.  On WinXP and presumably later, this is 0x40,
160*40796051SNamjae Jeon  *   i.e. there are 16 extra bytes between this record and the client
161*40796051SNamjae Jeon  *   array.  This probably means that the RESTART_AREA record is actually
162*40796051SNamjae Jeon  *   bigger in WinXP and later.
163*40796051SNamjae Jeon  * @file_size: Usable byte size of the log file.
164*40796051SNamjae Jeon  *   If the restart_area_offset + the offset of the file_size are > 510
165*40796051SNamjae Jeon  *   then corruption has occurred.  This is the very first check when
166*40796051SNamjae Jeon  *   starting with the restart_area as if it fails it means that some of
167*40796051SNamjae Jeon  *   the above values will be corrupted by the multi sector transfer
168*40796051SNamjae Jeon  *   protection.  The file_size has to be rounded down to be a multiple
169*40796051SNamjae Jeon  *   of the log_page_size in the RESTART_PAGE_HEADER and then it has to be
170*40796051SNamjae Jeon  *   at least big enough to store the two restart pages and 48 (0x30) log
171*40796051SNamjae Jeon  *   record pages.
172*40796051SNamjae Jeon  * @last_lsn_data_length: Length of data of last LSN, not including the log
173*40796051SNamjae Jeon  *   record header.  On create set to 0.
174*40796051SNamjae Jeon  * @log_record_header_length: Byte size of the log record header.  If the
175*40796051SNamjae Jeon  *   version matches then check that the value of log_record_header_length
176*40796051SNamjae Jeon  *   is a multiple of 8, i.e. (log_record_header_length + 7) & ~7 ==
177*40796051SNamjae Jeon  *   log_record_header_length.  When creating set it to
178*40796051SNamjae Jeon  *   sizeof(LOG_RECORD_HEADER), aligned to 8 bytes.
179*40796051SNamjae Jeon  * @log_page_data_offset: Offset to the start of data in a log record page.
180*40796051SNamjae Jeon  *   Must be a multiple of 8.  On create set it to immediately after
181*40796051SNamjae Jeon  *   the update sequence array of the log record page.
182*40796051SNamjae Jeon  * @restart_log_open_count: A counter that gets incremented every time
183*40796051SNamjae Jeon  *   the logfile is restarted which happens at mount time when the logfile
184*40796051SNamjae Jeon  *   is opened. When creating set to a random value.  Win2k sets it to
185*40796051SNamjae Jeon  *   the low 32 bits of the current system time in NTFS format (see time.h).
186*40796051SNamjae Jeon  * @reserved: Reserved/alignment to 8-byte boundary.
1871e9ea7e0SNamjae Jeon  */
188*40796051SNamjae Jeon struct restart_area {
189*40796051SNamjae Jeon 	__le64 current_lsn;
190*40796051SNamjae Jeon 	__le16 log_clients;
191*40796051SNamjae Jeon 	__le16 client_free_list;
192*40796051SNamjae Jeon 	__le16 client_in_use_list;
193*40796051SNamjae Jeon 	__le16 flags;
194*40796051SNamjae Jeon 	__le32 seq_number_bits;
195*40796051SNamjae Jeon 	__le16 restart_area_length;
196*40796051SNamjae Jeon 	__le16 client_array_offset;
197*40796051SNamjae Jeon 	__le64 file_size;
198*40796051SNamjae Jeon 	__le32 last_lsn_data_length;
199*40796051SNamjae Jeon 	__le16 log_record_header_length;
200*40796051SNamjae Jeon 	__le16 log_page_data_offset;
201*40796051SNamjae Jeon 	__le32 restart_log_open_count;
202*40796051SNamjae Jeon 	__le32 reserved;
203*40796051SNamjae Jeon } __packed;
2041e9ea7e0SNamjae Jeon 
2051e9ea7e0SNamjae Jeon /*
2061e9ea7e0SNamjae Jeon  * Log client record.  The offset of this record is found by adding the offset
2071e9ea7e0SNamjae Jeon  * of the RESTART_AREA to the client_array_offset value found in it.
208*40796051SNamjae Jeon  *
209*40796051SNamjae Jeon  * @oldest_lsn: Oldest LSN needed by this client.  On create set to 0.
210*40796051SNamjae Jeon  * @client_restart_lsn: LSN at which this client needs to restart
211*40796051SNamjae Jeon  *   the volume, i.e. the current position within the log file.
212*40796051SNamjae Jeon  *   At present, if clean this should = current_lsn in restart area but it
213*40796051SNamjae Jeon  *   probably also = current_lsn when dirty most of the time.
214*40796051SNamjae Jeon  *   At create set to 0.
215*40796051SNamjae Jeon  * @prev_client: The offset to the previous log client record in the array
216*40796051SNamjae Jeon  *   of log client records.  LOGFILE_NO_CLIENT means there is no previous
217*40796051SNamjae Jeon  *   client record, i.e. this is the first one.  This is always
218*40796051SNamjae Jeon  *   LOGFILE_NO_CLIENT.
219*40796051SNamjae Jeon  * @next_client: The offset to the next log client record in the array of
220*40796051SNamjae Jeon  *   log client records.  LOGFILE_NO_CLIENT means there are no next client
221*40796051SNamjae Jeon  *   records, i.e. this is the last one.  This is always LOGFILE_NO_CLIENT.
222*40796051SNamjae Jeon  * @seq_number: On Win2k and presumably earlier, this is set to zero every
223*40796051SNamjae Jeon  *   time the logfile is restarted and it is incremented when the logfile
224*40796051SNamjae Jeon  *   is closed at dismount time.  Thus it is 0 when dirty and 1 when clean.
225*40796051SNamjae Jeon  *   On WinXP and presumably later, this is always 0.
226*40796051SNamjae Jeon  * @reserved[6]: Reserved/alignment.
227*40796051SNamjae Jeon  * @client_name_length: Length of client name in bytes.  Should always be 8.
228*40796051SNamjae Jeon  * @client_name[64]: Name of the client in Unicode. Should always be "NTFS"
229*40796051SNamjae Jeon  *   with the remaining bytes set to 0.
2301e9ea7e0SNamjae Jeon  */
231*40796051SNamjae Jeon struct log_client_record {
232*40796051SNamjae Jeon 	__le64 oldest_lsn;
233*40796051SNamjae Jeon 	__le64 client_restart_lsn;
234*40796051SNamjae Jeon 	__le16 prev_client;
235*40796051SNamjae Jeon 	__le16 next_client;
236*40796051SNamjae Jeon 	__le16 seq_number;
237*40796051SNamjae Jeon 	u8 reserved[6];
238*40796051SNamjae Jeon 	__le32 client_name_length;
239*40796051SNamjae Jeon 	__le16 client_name[64];
240*40796051SNamjae Jeon } __packed;
2411e9ea7e0SNamjae Jeon 
242*40796051SNamjae Jeon bool ntfs_check_logfile(struct inode *log_vi,
243*40796051SNamjae Jeon 		struct restart_page_header **rp);
244*40796051SNamjae Jeon bool ntfs_empty_logfile(struct inode *log_vi);
2451e9ea7e0SNamjae Jeon #endif /* _LINUX_NTFS_LOGFILE_H */
246