2 * logfile.h - Defines for NTFS kernel journal ($LogFile) handling. Part of
3 * the Linux-NTFS project.
5 * Copyright (c) 2000-2004 Anton Altaparmakov
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifndef _LINUX_NTFS_LOGFILE_H
24 #define _LINUX_NTFS_LOGFILE_H
35 * Journal ($LogFile) organization:
37 * Two restart areas present in the first two pages (restart pages, one restart
38 * area in each page). When the volume is dismounted they should be identical,
39 * except for the update sequence array which usually has a different update
42 * These are followed by log records organized in pages headed by a log record
43 * header going up to log file size. Not all pages contain log records when a
44 * volume is first formatted, but as the volume ages, all records will be used.
45 * When the log file fills up, the records at the beginning are purged (by
46 * modifying the oldest_lsn to a higher value presumably) and writing begins
47 * at the beginning of the file. Effectively, the log file is viewed as a
50 * NOTE: Windows NT, 2000, and XP all use log file version 1.1 but they accept
51 * versions <= 1.x, including 0.-1. (Yes, that is a minus one in there!) We
52 * probably only want to support 1.1 as this seems to be the current version
53 * and we don't know how that differs from the older versions. The only
54 * exception is if the journal is clean as marked by the two restart pages
55 * then it doesn't matter whether we are on an earlier version. We can just
56 * reinitialize the logfile and start again with version 1.1.
59 /* Some $LogFile related constants. */
60 #define MaxLogFileSize 0x100000000ULL
61 #define DefaultLogPageSize 4096
62 #define MinLogRecordPages 48
65 * Log file restart page header (begins the restart area).
69 /* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */
70 /* 0*/ NTFS_RECORD_TYPES magic;/* The magic is "RSTR". */
71 /* 4*/ u16 usa_ofs; /* See NTFS_RECORD definition in layout.h.
72 When creating, set this to be immediately
73 after this header structure (without any
75 /* 6*/ u16 usa_count; /* See NTFS_RECORD definition in layout.h. */
77 /* 8*/ LSN chkdsk_lsn; /* The last log file sequence number found by
78 chkdsk. Only used when the magic is changed
79 to "CHKD". Otherwise this is zero. */
80 /* 16*/ u32 system_page_size; /* Byte size of system pages when the log file
81 was created, has to be >= 512 and a power of
82 2. Use this to calculate the required size
83 of the usa (usa_count) and add it to usa_ofs.
84 Then verify that the result is less than the
85 value of the restart_area_offset. */
86 /* 20*/ u32 log_page_size; /* Byte size of log file pages, has to be >=
87 512 and a power of 2. The default is 4096
88 and is used when the system page size is
89 between 4096 and 8192. Otherwise this is
90 set to the system page size instead. */
91 /* 24*/ u16 restart_area_offset;/* Byte offset from the start of this header to
92 the RESTART_AREA. Value has to be aligned
93 to 8-byte boundary. When creating, set this
94 to be after the usa. */
95 /* 26*/ s16 minor_ver; /* Log file minor version. Only check if major
97 /* 28*/ s16 major_ver; /* Log file major version. We only support
99 /* sizeof() = 30 (0x1e) bytes */
100 } __attribute__ ((__packed__)) RESTART_PAGE_HEADER;
103 * Constant for the log client indices meaning that there are no client records
104 * in this particular client array. Also inside the client records themselves,
105 * this means that there are no client records preceding or following this one.
107 #define LOGFILE_NO_CLIENT const_cpu_to_le16(0xffff)
108 #define LOGFILE_NO_CLIENT_CPU 0xffff
111 * These are the so far known RESTART_AREA_* flags (16-bit) which contain
112 * information about the log file in which they are present.
115 RESTART_VOLUME_IS_CLEAN = const_cpu_to_le16(0x0002),
116 REST_AREA_SPACE_FILLER = 0xffff /* Just to make flags 16-bit. */
117 } __attribute__ ((__packed__)) RESTART_AREA_FLAGS;
120 * Log file restart area record. The offset of this record is found by adding
121 * the offset of the RESTART_PAGE_HEADER to the restart_area_offset value found
122 * in it. See notes at restart_area_offset above.
126 /* 0*/ LSN current_lsn; /* The current, i.e. last LSN inside the log
127 when the restart area was last written.
128 This happens often but what is the interval?
129 Is it just fixed time or is it every time a
130 check point is written or somethine else?
131 On create set to 0. */
132 /* 8*/ u16 log_clients; /* Number of log client records in the array of
133 log client records which follows this
134 restart area. Must be 1. */
135 /* 10*/ u16 client_free_list; /* The index of the first free log client record
136 in the array of log client records.
137 LOGFILE_NO_CLIENT means that there are no
138 free log client records in the array.
139 If != LOGFILE_NO_CLIENT, check that
140 log_clients > client_free_list. On Win2k
141 and presumably earlier, on a clean volume
142 this is != LOGFILE_NO_CLIENT, and it should
143 be 0, i.e. the first (and only) client
144 record is free and thus the logfile is
145 closed and hence clean. A dirty volume
146 would have left the logfile open and hence
147 this would be LOGFILE_NO_CLIENT. On WinXP
148 and presumably later, the logfile is always
149 open, even on clean shutdown so this should
150 always be LOGFILE_NO_CLIENT. */
151 /* 12*/ u16 client_in_use_list; /* The index of the first in-use log client
152 record in the array of log client records.
153 LOGFILE_NO_CLIENT means that there are no
154 in-use log client records in the array. If
155 != LOGFILE_NO_CLIENT check that log_clients
156 > client_in_use_list. On Win2k and
157 presumably earlier, on a clean volume this
158 is LOGFILE_NO_CLIENT, i.e. there are no
159 client records in use and thus the logfile
160 is closed and hence clean. A dirty volume
161 would have left the logfile open and hence
162 this would be != LOGFILE_NO_CLIENT, and it
163 should be 0, i.e. the first (and only)
164 client record is in use. On WinXP and
165 presumably later, the logfile is always
166 open, even on clean shutdown so this should
168 /* 14*/ RESTART_AREA_FLAGS flags;/* Flags modifying LFS behaviour. On Win2k
169 and presumably earlier this is always 0. On
170 WinXP and presumably later, if the logfile
171 was shutdown cleanly, the second bit,
172 RESTART_VOLUME_IS_CLEAN, is set. This bit
173 is cleared when the volume is mounted by
174 WinXP and set when the volume is dismounted,
175 thus if the logfile is dirty, this bit is
176 clear. Thus we don't need to check the
177 Windows version to determine if the logfile
178 is clean. Instead if the logfile is closed,
179 we know it must be clean. If it is open and
180 this bit is set, we also know it must be
181 clean. If on the other hand the logfile is
182 open and this bit is clear, we can be almost
183 certain that the logfile is dirty. */
184 /* 16*/ u32 seq_number_bits; /* How many bits to use for the sequence
185 number. This is calculated as 67 - the
186 number of bits required to store the logfile
187 size in bytes and this can be used in with
188 the specified file_size as a consistency
190 /* 20*/ u16 restart_area_length;/* Length of the restart area including the
191 client array. Following checks required if
192 version matches. Otherwise, skip them.
193 restart_area_offset + restart_area_length
194 has to be <= system_page_size. Also,
195 restart_area_length has to be >=
196 client_array_offset + (log_clients *
197 sizeof(log client record)). */
198 /* 22*/ u16 client_array_offset;/* Offset from the start of this record to
199 the first log client record if versions are
200 matched. When creating, set this to be
201 after this restart area structure, aligned
202 to 8-bytes boundary. If the versions do not
203 match, this is ignored and the offset is
204 assumed to be (sizeof(RESTART_AREA) + 7) &
205 ~7, i.e. rounded up to first 8-byte
206 boundary. Either way, client_array_offset
207 has to be aligned to an 8-byte boundary.
208 Also, restart_area_offset +
209 client_array_offset has to be <= 510.
210 Finally, client_array_offset + (log_clients
211 * sizeof(log client record)) has to be <=
212 system_page_size. On Win2k and presumably
213 earlier, this is 0x30, i.e. immediately
214 following this record. On WinXP and
215 presumably later, this is 0x40, i.e. there
216 are 16 extra bytes between this record and
217 the client array. This probably means that
218 the RESTART_AREA record is actually bigger
219 in WinXP and later. */
220 /* 24*/ s64 file_size; /* Usable byte size of the log file. If the
221 restart_area_offset + the offset of the
222 file_size are > 510 then corruption has
223 occured. This is the very first check when
224 starting with the restart_area as if it
225 fails it means that some of the above values
226 will be corrupted by the multi sector
227 transfer protection. The file_size has to
228 be rounded down to be a multiple of the
229 log_page_size in the RESTART_PAGE_HEADER and
230 then it has to be at least big enough to
231 store the two restart pages and 48 (0x30)
233 /* 32*/ u32 last_lsn_data_length;/* Length of data of last LSN, not including
234 the log record header. On create set to
236 /* 36*/ u16 log_record_header_length;/* Byte size of the log record header. If
237 the version matches then check that the
238 value of log_record_header_length is a
240 (log_record_header_length + 7) & ~7 ==
241 log_record_header_length. When creating set
242 it to sizeof(LOG_RECORD_HEADER), aligned to
244 /* 38*/ u16 log_page_data_offset;/* Offset to the start of data in a log record
245 page. Must be a multiple of 8. On create
246 set it to immediately after the update
247 sequence array of the log record page. */
248 /* 40*/ u32 restart_log_open_count;/* A counter that gets incremented every time
249 the logfile is restarted which happens at
250 mount time when the logfile is opened. When
251 creating set to a random value. Win2k sets
252 it to the low 32 bits of the current system
253 time in NTFS format (see time.h). */
254 /* 44*/ u32 reserved; /* Reserved/alignment to 8-byte boundary. */
255 /* sizeof() = 48 (0x30) bytes */
256 } __attribute__ ((__packed__)) RESTART_AREA;
259 * Log client record. The offset of this record is found by adding the offset
260 * of the RESTART_AREA to the client_array_offset value found in it.
264 /* 0*/ LSN oldest_lsn; /* Oldest LSN needed by this client. On create
266 /* 8*/ LSN client_restart_lsn; /* LSN at which this client needs to restart
267 the volume, i.e. the current position within
268 the log file. At present, if clean this
269 should = current_lsn in restart area but it
270 probably also = current_lsn when dirty most
271 of the time. At create set to 0. */
272 /* 16*/ u16 prev_client; /* The offset to the previous log client record
273 in the array of log client records.
274 LOGFILE_NO_CLIENT means there is no previous
275 client record, i.e. this is the first one.
276 This is always LOGFILE_NO_CLIENT. */
277 /* 18*/ u16 next_client; /* The offset to the next log client record in
278 the array of log client records.
279 LOGFILE_NO_CLIENT means there are no next
280 client records, i.e. this is the last one.
281 This is always LOGFILE_NO_CLIENT. */
282 /* 20*/ u16 seq_number; /* On Win2k and presumably earlier, this is set
283 to zero every time the logfile is restarted
284 and it is incremented when the logfile is
285 closed at dismount time. Thus it is 0 when
286 dirty and 1 when clean. On WinXP and
287 presumably later, this is always 0. */
288 /* 22*/ u8 reserved[6]; /* Reserved/alignment. */
289 /* 28*/ u32 client_name_length; /* Length of client name in bytes. Should
291 /* 32*/ ntfschar client_name[64];/* Name of the client in Unicode. Should
292 always be "NTFS" with the remaining bytes
294 /* sizeof() = 160 (0xa0) bytes */
295 } __attribute__ ((__packed__)) LOG_CLIENT_RECORD;
297 extern BOOL ntfs_check_logfile(struct inode *log_vi);
299 extern BOOL ntfs_is_logfile_clean(struct inode *log_vi);
301 extern BOOL ntfs_empty_logfile(struct inode *log_vi);
305 #endif /* _LINUX_NTFS_LOGFILE_H */