• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * logfile.c - NTFS journal handling. Originated from the Linux-NTFS project.
3  *
4  * Copyright (c) 2002-2005 Anton Altaparmakov
5  * Copyright (c) 2005 Yura Pakhuchiy
6  * Copyright (c) 2005-2009 Szabolcs Szakacsits
7  *
8  * This program/include file is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as published
10  * by the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program/include file is distributed in the hope that it will be
14  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program (in the main directory of the NTFS-3G
20  * distribution in the file COPYING); if not, write to the Free Software
21  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 #ifdef HAVE_STRING_H
32 #include <string.h>
33 #endif
34 #ifdef HAVE_ERRNO_H
35 #include <errno.h>
36 #endif
37 
38 #include "attrib.h"
39 #include "debug.h"
40 #include "logfile.h"
41 #include "volume.h"
42 #include "mst.h"
43 #include "logging.h"
44 #include "misc.h"
45 
46 /**
47  * ntfs_check_restart_page_header - check the page header for consistency
48  * @rp:		restart page header to check
49  * @pos:	position in logfile at which the restart page header resides
50  *
51  * Check the restart page header @rp for consistency and return TRUE if it is
52  * consistent and FALSE otherwise.
53  *
54  * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
55  * require the full restart page.
56  */
ntfs_check_restart_page_header(RESTART_PAGE_HEADER * rp,s64 pos)57 static BOOL ntfs_check_restart_page_header(RESTART_PAGE_HEADER *rp, s64 pos)
58 {
59 	u32 logfile_system_page_size, logfile_log_page_size;
60 	u16 ra_ofs, usa_count, usa_ofs, usa_end = 0;
61 	BOOL have_usa = TRUE;
62 
63 	ntfs_log_trace("Entering.\n");
64 	/*
65 	 * If the system or log page sizes are smaller than the ntfs block size
66 	 * or either is not a power of 2 we cannot handle this log file.
67 	 */
68 	logfile_system_page_size = le32_to_cpu(rp->system_page_size);
69 	logfile_log_page_size = le32_to_cpu(rp->log_page_size);
70 	if (logfile_system_page_size < NTFS_BLOCK_SIZE ||
71 			logfile_log_page_size < NTFS_BLOCK_SIZE ||
72 			logfile_system_page_size &
73 			(logfile_system_page_size - 1) ||
74 			logfile_log_page_size & (logfile_log_page_size - 1)) {
75 		ntfs_log_error("$LogFile uses unsupported page size.\n");
76 		return FALSE;
77 	}
78 	/*
79 	 * We must be either at !pos (1st restart page) or at pos = system page
80 	 * size (2nd restart page).
81 	 */
82 	if (pos && pos != logfile_system_page_size) {
83 		ntfs_log_error("Found restart area in incorrect "
84 				"position in $LogFile.\n");
85 		return FALSE;
86 	}
87 	/*
88 	 * We only know how to handle version 1.1 and 2.0, though
89 	 * version 2.0 is probably related to cached metadata in
90 	 * Windows 8, and we will refuse to mount.
91 	 * Nevertheless, do all the relevant checks before rejecting.
92 	 */
93 	if (((rp->major_ver != const_cpu_to_sle16(1))
94 			 || (rp->minor_ver != const_cpu_to_sle16(1)))
95 	   && ((rp->major_ver != const_cpu_to_sle16(2))
96 			 || (rp->minor_ver != const_cpu_to_sle16(0)))) {
97 		ntfs_log_error("$LogFile version %i.%i is not "
98 				"supported.\n   (This driver supports version "
99 				"1.1 and 2.0 only.)\n",
100 					(int)sle16_to_cpu(rp->major_ver),
101 					(int)sle16_to_cpu(rp->minor_ver));
102 		return FALSE;
103 	}
104 	/*
105 	 * If chkdsk has been run the restart page may not be protected by an
106 	 * update sequence array.
107 	 */
108 	if (ntfs_is_chkd_record(rp->magic) && !le16_to_cpu(rp->usa_count)) {
109 		have_usa = FALSE;
110 		goto skip_usa_checks;
111 	}
112 	/* Verify the size of the update sequence array. */
113 	usa_count = 1 + (logfile_system_page_size >> NTFS_BLOCK_SIZE_BITS);
114 	if (usa_count != le16_to_cpu(rp->usa_count)) {
115 		ntfs_log_error("$LogFile restart page specifies "
116 				"inconsistent update sequence array count.\n");
117 		return FALSE;
118 	}
119 	/* Verify the position of the update sequence array. */
120 	usa_ofs = le16_to_cpu(rp->usa_ofs);
121 	usa_end = usa_ofs + usa_count * sizeof(u16);
122 	if (usa_ofs < offsetof(RESTART_PAGE_HEADER, usn) ||
123 			usa_end > NTFS_BLOCK_SIZE - sizeof(u16)) {
124 		ntfs_log_error("$LogFile restart page specifies "
125 				"inconsistent update sequence array offset.\n");
126 		return FALSE;
127 	}
128 skip_usa_checks:
129 	/*
130 	 * Verify the position of the restart area.  It must be:
131 	 *	- aligned to 8-byte boundary,
132 	 *	- after the update sequence array, and
133 	 *	- within the system page size.
134 	 */
135 	ra_ofs = le16_to_cpu(rp->restart_area_offset);
136 	if (ra_ofs & 7 || (have_usa ? ra_ofs < usa_end :
137 			ra_ofs < offsetof(RESTART_PAGE_HEADER, usn)) ||
138 			ra_ofs > logfile_system_page_size) {
139 		ntfs_log_error("$LogFile restart page specifies "
140 				"inconsistent restart area offset.\n");
141 		return FALSE;
142 	}
143 	/*
144 	 * Only restart pages modified by chkdsk are allowed to have chkdsk_lsn
145 	 * set.
146 	 */
147 	if (!ntfs_is_chkd_record(rp->magic) && sle64_to_cpu(rp->chkdsk_lsn)) {
148 		ntfs_log_error("$LogFile restart page is not modified "
149 				"by chkdsk but a chkdsk LSN is specified.\n");
150 		return FALSE;
151 	}
152 	ntfs_log_trace("Done.\n");
153 	return TRUE;
154 }
155 
156 /**
157  * ntfs_check_restart_area - check the restart area for consistency
158  * @rp:		restart page whose restart area to check
159  *
160  * Check the restart area of the restart page @rp for consistency and return
161  * TRUE if it is consistent and FALSE otherwise.
162  *
163  * This function assumes that the restart page header has already been
164  * consistency checked.
165  *
166  * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
167  * require the full restart page.
168  */
ntfs_check_restart_area(RESTART_PAGE_HEADER * rp)169 static BOOL ntfs_check_restart_area(RESTART_PAGE_HEADER *rp)
170 {
171 	u64 file_size;
172 	RESTART_AREA *ra;
173 	u16 ra_ofs, ra_len, ca_ofs;
174 	u8 fs_bits;
175 
176 	ntfs_log_trace("Entering.\n");
177 	ra_ofs = le16_to_cpu(rp->restart_area_offset);
178 	ra = (RESTART_AREA*)((u8*)rp + ra_ofs);
179 	/*
180 	 * Everything before ra->file_size must be before the first word
181 	 * protected by an update sequence number.  This ensures that it is
182 	 * safe to access ra->client_array_offset.
183 	 */
184 	if (ra_ofs + offsetof(RESTART_AREA, file_size) >
185 			NTFS_BLOCK_SIZE - sizeof(u16)) {
186 		ntfs_log_error("$LogFile restart area specifies "
187 				"inconsistent file offset.\n");
188 		return FALSE;
189 	}
190 	/*
191 	 * Now that we can access ra->client_array_offset, make sure everything
192 	 * up to the log client array is before the first word protected by an
193 	 * update sequence number.  This ensures we can access all of the
194 	 * restart area elements safely.  Also, the client array offset must be
195 	 * aligned to an 8-byte boundary.
196 	 */
197 	ca_ofs = le16_to_cpu(ra->client_array_offset);
198 	if (((ca_ofs + 7) & ~7) != ca_ofs ||
199 			ra_ofs + ca_ofs > (u16)(NTFS_BLOCK_SIZE -
200 			sizeof(u16))) {
201 		ntfs_log_error("$LogFile restart area specifies "
202 				"inconsistent client array offset.\n");
203 		return FALSE;
204 	}
205 	/*
206 	 * The restart area must end within the system page size both when
207 	 * calculated manually and as specified by ra->restart_area_length.
208 	 * Also, the calculated length must not exceed the specified length.
209 	 */
210 	ra_len = ca_ofs + le16_to_cpu(ra->log_clients) *
211 			sizeof(LOG_CLIENT_RECORD);
212 	if ((u32)(ra_ofs + ra_len) > le32_to_cpu(rp->system_page_size) ||
213 			(u32)(ra_ofs + le16_to_cpu(ra->restart_area_length)) >
214 			le32_to_cpu(rp->system_page_size) ||
215 			ra_len > le16_to_cpu(ra->restart_area_length)) {
216 		ntfs_log_error("$LogFile restart area is out of bounds "
217 				"of the system page size specified by the "
218 				"restart page header and/or the specified "
219 				"restart area length is inconsistent.\n");
220 		return FALSE;
221 	}
222 	/*
223 	 * The ra->client_free_list and ra->client_in_use_list must be either
224 	 * LOGFILE_NO_CLIENT or less than ra->log_clients or they are
225 	 * overflowing the client array.
226 	 */
227 	if ((ra->client_free_list != LOGFILE_NO_CLIENT &&
228 			le16_to_cpu(ra->client_free_list) >=
229 			le16_to_cpu(ra->log_clients)) ||
230 			(ra->client_in_use_list != LOGFILE_NO_CLIENT &&
231 			le16_to_cpu(ra->client_in_use_list) >=
232 			le16_to_cpu(ra->log_clients))) {
233 		ntfs_log_error("$LogFile restart area specifies "
234 				"overflowing client free and/or in use lists.\n");
235 		return FALSE;
236 	}
237 	/*
238 	 * Check ra->seq_number_bits against ra->file_size for consistency.
239 	 * We cannot just use ffs() because the file size is not a power of 2.
240 	 */
241 	file_size = (u64)sle64_to_cpu(ra->file_size);
242 	fs_bits = 0;
243 	while (file_size) {
244 		file_size >>= 1;
245 		fs_bits++;
246 	}
247 	if (le32_to_cpu(ra->seq_number_bits) != (u32)(67 - fs_bits)) {
248 		ntfs_log_error("$LogFile restart area specifies "
249 				"inconsistent sequence number bits.\n");
250 		return FALSE;
251 	}
252 	/* The log record header length must be a multiple of 8. */
253 	if (((le16_to_cpu(ra->log_record_header_length) + 7) & ~7) !=
254 			le16_to_cpu(ra->log_record_header_length)) {
255 		ntfs_log_error("$LogFile restart area specifies "
256 				"inconsistent log record header length.\n");
257 		return FALSE;
258 	}
259 	/* Ditto for the log page data offset. */
260 	if (((le16_to_cpu(ra->log_page_data_offset) + 7) & ~7) !=
261 			le16_to_cpu(ra->log_page_data_offset)) {
262 		ntfs_log_error("$LogFile restart area specifies "
263 				"inconsistent log page data offset.\n");
264 		return FALSE;
265 	}
266 	ntfs_log_trace("Done.\n");
267 	return TRUE;
268 }
269 
270 /**
271  * ntfs_check_log_client_array - check the log client array for consistency
272  * @rp:		restart page whose log client array to check
273  *
274  * Check the log client array of the restart page @rp for consistency and
275  * return TRUE if it is consistent and FALSE otherwise.
276  *
277  * This function assumes that the restart page header and the restart area have
278  * already been consistency checked.
279  *
280  * Unlike ntfs_check_restart_page_header() and ntfs_check_restart_area(), this
281  * function needs @rp->system_page_size bytes in @rp, i.e. it requires the full
282  * restart page and the page must be multi sector transfer deprotected.
283  */
ntfs_check_log_client_array(RESTART_PAGE_HEADER * rp)284 static BOOL ntfs_check_log_client_array(RESTART_PAGE_HEADER *rp)
285 {
286 	RESTART_AREA *ra;
287 	LOG_CLIENT_RECORD *ca, *cr;
288 	u16 nr_clients, idx;
289 	BOOL in_free_list, idx_is_first;
290 	u32 offset_clients;
291 
292 	ntfs_log_trace("Entering.\n");
293 		/* The restart area must be fully within page */
294 	if ((le16_to_cpu(rp->restart_area_offset) + sizeof(RESTART_AREA))
295 			> le32_to_cpu(rp->system_page_size))
296 		goto err_out;
297 	ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
298 	offset_clients = le16_to_cpu(rp->restart_area_offset)
299 			+ le16_to_cpu(ra->client_array_offset);
300 		/* The clients' records must begin within page */
301 	if (offset_clients >= le32_to_cpu(rp->system_page_size))
302 		goto err_out;
303 	ca = (LOG_CLIENT_RECORD*)((u8*)ra +
304 			le16_to_cpu(ra->client_array_offset));
305 	/*
306 	 * Check the ra->client_free_list first and then check the
307 	 * ra->client_in_use_list.  Check each of the log client records in
308 	 * each of the lists and check that the array does not overflow the
309 	 * ra->log_clients value.  Also keep track of the number of records
310 	 * visited as there cannot be more than ra->log_clients records and
311 	 * that way we detect eventual loops in within a list.
312 	 */
313 	nr_clients = le16_to_cpu(ra->log_clients);
314 	idx = le16_to_cpu(ra->client_free_list);
315 	in_free_list = TRUE;
316 check_list:
317 	for (idx_is_first = TRUE; idx != LOGFILE_NO_CLIENT_CPU; nr_clients--,
318 			idx = le16_to_cpu(cr->next_client)) {
319 		if (!nr_clients || idx >= le16_to_cpu(ra->log_clients))
320 			goto err_out;
321 			/* The client record must be fully within page */
322 		if ((offset_clients + (idx + 1)*sizeof(LOG_CLIENT_RECORD))
323 				> le32_to_cpu(rp->system_page_size))
324 			goto err_out;
325 		/* Set @cr to the current log client record. */
326 		cr = ca + idx;
327 		/* The first log client record must not have a prev_client. */
328 		if (idx_is_first) {
329 			if (cr->prev_client != LOGFILE_NO_CLIENT)
330 				goto err_out;
331 			idx_is_first = FALSE;
332 		}
333 	}
334 	/* Switch to and check the in use list if we just did the free list. */
335 	if (in_free_list) {
336 		in_free_list = FALSE;
337 		idx = le16_to_cpu(ra->client_in_use_list);
338 		goto check_list;
339 	}
340 	ntfs_log_trace("Done.\n");
341 	return TRUE;
342 err_out:
343 	ntfs_log_error("$LogFile log client array is corrupt.\n");
344 	return FALSE;
345 }
346 
347 /**
348  * ntfs_check_and_load_restart_page - check the restart page for consistency
349  * @log_na:	opened ntfs attribute for journal $LogFile
350  * @rp:		restart page to check
351  * @pos:	position in @log_na at which the restart page resides
352  * @wrp:       [OUT] copy of the multi sector transfer deprotected restart page
353  * @lsn:       [OUT] set to the current logfile lsn on success
354  *
355  * Check the restart page @rp for consistency and return 0 if it is consistent
356  * and errno otherwise.  The restart page may have been modified by chkdsk in
357  * which case its magic is CHKD instead of RSTR.
358  *
359  * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
360  * require the full restart page.
361  *
362  * If @wrp is not NULL, on success, *@wrp will point to a buffer containing a
363  * copy of the complete multi sector transfer deprotected page.  On failure,
364  * *@wrp is undefined.
365  *
366  * Similarly, if @lsn is not NULL, on success *@lsn will be set to the current
367  * logfile lsn according to this restart page.  On failure, *@lsn is undefined.
368  *
369  * The following error codes are defined:
370  *     EINVAL - The restart page is inconsistent.
371  *     ENOMEM - Not enough memory to load the restart page.
372  *     EIO    - Failed to reading from $LogFile.
373  */
ntfs_check_and_load_restart_page(ntfs_attr * log_na,RESTART_PAGE_HEADER * rp,s64 pos,RESTART_PAGE_HEADER ** wrp,LSN * lsn)374 static int ntfs_check_and_load_restart_page(ntfs_attr *log_na,
375 		RESTART_PAGE_HEADER *rp, s64 pos, RESTART_PAGE_HEADER **wrp,
376 		LSN *lsn)
377 {
378 	RESTART_AREA *ra;
379 	RESTART_PAGE_HEADER *trp;
380 	int err;
381 
382 	ntfs_log_trace("Entering.\n");
383 	/* Check the restart page header for consistency. */
384 	if (!ntfs_check_restart_page_header(rp, pos)) {
385 		/* Error output already done inside the function. */
386 		return EINVAL;
387 	}
388 	/* Check the restart area for consistency. */
389 	if (!ntfs_check_restart_area(rp)) {
390 		/* Error output already done inside the function. */
391 		return EINVAL;
392 	}
393 	ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
394 	/*
395 	 * Allocate a buffer to store the whole restart page so we can multi
396 	 * sector transfer deprotect it.
397 	 * For safety, make sure this is consistent with the usa_count
398 	 * and shorter than the full log size
399 	 */
400 	if ((le32_to_cpu(rp->system_page_size)
401 			> (u32)(le16_to_cpu(rp->usa_count) - 1)*NTFS_BLOCK_SIZE)
402 	   || (le32_to_cpu(rp->system_page_size)
403 			> le64_to_cpu(log_na->data_size)))
404 		return (EINVAL);
405 	trp = ntfs_malloc(le32_to_cpu(rp->system_page_size));
406 	if (!trp)
407 		return errno;
408 	/*
409 	 * Read the whole of the restart page into the buffer.  If it fits
410 	 * completely inside @rp, just copy it from there.  Otherwise read it
411 	 * from disk.
412 	 */
413 	if (le32_to_cpu(rp->system_page_size) <= NTFS_BLOCK_SIZE)
414 		memcpy(trp, rp, le32_to_cpu(rp->system_page_size));
415 	else if (ntfs_attr_pread(log_na, pos,
416 			le32_to_cpu(rp->system_page_size), trp) !=
417 			le32_to_cpu(rp->system_page_size)) {
418 		err = errno;
419 		ntfs_log_error("Failed to read whole restart page into the "
420 				"buffer.\n");
421 		if (err != ENOMEM)
422 			err = EIO;
423 		goto err_out;
424 	}
425 	/*
426 	 * Perform the multi sector transfer deprotection on the buffer if the
427 	 * restart page is protected.
428 	 */
429 	if ((!ntfs_is_chkd_record(trp->magic) || le16_to_cpu(trp->usa_count))
430 			&& ntfs_mst_post_read_fixup((NTFS_RECORD*)trp,
431 			le32_to_cpu(rp->system_page_size))) {
432 		/*
433 		 * A multi sector tranfer error was detected.  We only need to
434 		 * abort if the restart page contents exceed the multi sector
435 		 * transfer fixup of the first sector.
436 		 */
437 		if (le16_to_cpu(rp->restart_area_offset) +
438 				le16_to_cpu(ra->restart_area_length) >
439 				NTFS_BLOCK_SIZE - (int)sizeof(u16)) {
440 			ntfs_log_error("Multi sector transfer error "
441 				   "detected in $LogFile restart page.\n");
442 			err = EINVAL;
443 			goto err_out;
444 		}
445 	}
446 	/*
447 	 * If the restart page is modified by chkdsk or there are no active
448 	 * logfile clients, the logfile is consistent.  Otherwise, need to
449 	 * check the log client records for consistency, too.
450 	 */
451 	err = 0;
452 	if (ntfs_is_rstr_record(rp->magic) &&
453 			ra->client_in_use_list != LOGFILE_NO_CLIENT) {
454 		if (!ntfs_check_log_client_array(trp)) {
455 			err = EINVAL;
456 			goto err_out;
457 		}
458 	}
459 	if (lsn) {
460 		if (ntfs_is_rstr_record(rp->magic))
461 			*lsn = sle64_to_cpu(ra->current_lsn);
462 		else /* if (ntfs_is_chkd_record(rp->magic)) */
463 			*lsn = sle64_to_cpu(rp->chkdsk_lsn);
464 	}
465 	ntfs_log_trace("Done.\n");
466 	if (wrp)
467 		*wrp = trp;
468 	else {
469 err_out:
470 		free(trp);
471 	}
472 	return err;
473 }
474 
475 /**
476  * ntfs_check_logfile - check in the journal if the volume is consistent
477  * @log_na:	ntfs attribute of loaded journal $LogFile to check
478  * @rp:         [OUT] on success this is a copy of the current restart page
479  *
480  * Check the $LogFile journal for consistency and return TRUE if it is
481  * consistent and FALSE if not.  On success, the current restart page is
482  * returned in *@rp.  Caller must call ntfs_free(*@rp) when finished with it.
483  *
484  * At present we only check the two restart pages and ignore the log record
485  * pages.
486  *
487  * Note that the MstProtected flag is not set on the $LogFile inode and hence
488  * when reading pages they are not deprotected.  This is because we do not know
489  * if the $LogFile was created on a system with a different page size to ours
490  * yet and mst deprotection would fail if our page size is smaller.
491  */
ntfs_check_logfile(ntfs_attr * log_na,RESTART_PAGE_HEADER ** rp)492 BOOL ntfs_check_logfile(ntfs_attr *log_na, RESTART_PAGE_HEADER **rp)
493 {
494 	s64 size, pos;
495 	LSN rstr1_lsn, rstr2_lsn;
496 	ntfs_volume *vol = log_na->ni->vol;
497 	u8 *kaddr = NULL;
498 	RESTART_PAGE_HEADER *rstr1_ph = NULL;
499 	RESTART_PAGE_HEADER *rstr2_ph = NULL;
500 	int log_page_size, err;
501 	BOOL logfile_is_empty = TRUE;
502 	u8 log_page_bits;
503 
504 	ntfs_log_trace("Entering.\n");
505 	/* An empty $LogFile must have been clean before it got emptied. */
506 	if (NVolLogFileEmpty(vol))
507 		goto is_empty;
508 	size = log_na->data_size;
509 	/* Make sure the file doesn't exceed the maximum allowed size. */
510 	if (size > (s64)MaxLogFileSize)
511 		size = MaxLogFileSize;
512 	log_page_size = DefaultLogPageSize;
513 	/*
514 	 * Use generic_ffs() instead of ffs() to enable the compiler to
515 	 * optimize log_page_size and log_page_bits into constants.
516 	 */
517 	log_page_bits = ffs(log_page_size) - 1;
518 	size &= ~(log_page_size - 1);
519 
520 	/*
521 	 * Ensure the log file is big enough to store at least the two restart
522 	 * pages and the minimum number of log record pages.
523 	 */
524 	if (size < log_page_size * 2 || (size - log_page_size * 2) >>
525 			log_page_bits < MinLogRecordPages) {
526 		ntfs_log_error("$LogFile is too small.\n");
527 		return FALSE;
528 	}
529 	/* Allocate memory for restart page. */
530 	kaddr = ntfs_malloc(NTFS_BLOCK_SIZE);
531 	if (!kaddr)
532 		return FALSE;
533 	/*
534 	 * Read through the file looking for a restart page.  Since the restart
535 	 * page header is at the beginning of a page we only need to search at
536 	 * what could be the beginning of a page (for each page size) rather
537 	 * than scanning the whole file byte by byte.  If all potential places
538 	 * contain empty and uninitialized records, the log file can be assumed
539 	 * to be empty.
540 	 */
541 	for (pos = 0; pos < size; pos <<= 1) {
542 		/*
543 		 * Read first NTFS_BLOCK_SIZE bytes of potential restart page.
544 		 */
545 		if (ntfs_attr_pread(log_na, pos, NTFS_BLOCK_SIZE, kaddr) !=
546 				NTFS_BLOCK_SIZE) {
547 			ntfs_log_error("Failed to read first NTFS_BLOCK_SIZE "
548 					"bytes of potential restart page.\n");
549 			goto err_out;
550 		}
551 
552 		/*
553 		 * A non-empty block means the logfile is not empty while an
554 		 * empty block after a non-empty block has been encountered
555 		 * means we are done.
556 		 */
557 		if (!ntfs_is_empty_recordp((le32*)kaddr))
558 			logfile_is_empty = FALSE;
559 		else if (!logfile_is_empty)
560 			break;
561 		/*
562 		 * A log record page means there cannot be a restart page after
563 		 * this so no need to continue searching.
564 		 */
565 		if (ntfs_is_rcrd_recordp((le32*)kaddr))
566 			break;
567 		/* If not a (modified by chkdsk) restart page, continue. */
568 		if (!ntfs_is_rstr_recordp((le32*)kaddr) &&
569 				!ntfs_is_chkd_recordp((le32*)kaddr)) {
570 			if (!pos)
571 				pos = NTFS_BLOCK_SIZE >> 1;
572 			continue;
573 		}
574 		/*
575 		 * Check the (modified by chkdsk) restart page for consistency
576 		 * and get a copy of the complete multi sector transfer
577 		 * deprotected restart page.
578 		 */
579 		err = ntfs_check_and_load_restart_page(log_na,
580 				(RESTART_PAGE_HEADER*)kaddr, pos,
581 				!rstr1_ph ? &rstr1_ph : &rstr2_ph,
582 				!rstr1_ph ? &rstr1_lsn : &rstr2_lsn);
583 		if (!err) {
584 			/*
585 			 * If we have now found the first (modified by chkdsk)
586 			 * restart page, continue looking for the second one.
587 			 */
588 			if (!pos) {
589 				pos = NTFS_BLOCK_SIZE >> 1;
590 				continue;
591 			}
592 			/*
593 			 * We have now found the second (modified by chkdsk)
594 			 * restart page, so we can stop looking.
595 			 */
596 			break;
597 		}
598 		/*
599 		 * Error output already done inside the function.  Note, we do
600 		 * not abort if the restart page was invalid as we might still
601 		 * find a valid one further in the file.
602 		 */
603 		if (err != EINVAL)
604 		      goto err_out;
605 		/* Continue looking. */
606 		if (!pos)
607 			pos = NTFS_BLOCK_SIZE >> 1;
608 	}
609 	if (kaddr) {
610 		free(kaddr);
611 		kaddr = NULL;
612 	}
613 	if (logfile_is_empty) {
614 		NVolSetLogFileEmpty(vol);
615 is_empty:
616 		ntfs_log_trace("Done.  ($LogFile is empty.)\n");
617 		return TRUE;
618 	}
619 	if (!rstr1_ph) {
620 		if (rstr2_ph)
621 			ntfs_log_error("BUG: rstr2_ph isn't NULL!\n");
622 		ntfs_log_error("Did not find any restart pages in "
623 			   "$LogFile and it was not empty.\n");
624 		return FALSE;
625 	}
626 	/* If both restart pages were found, use the more recent one. */
627 	if (rstr2_ph) {
628 		/*
629 		 * If the second restart area is more recent, switch to it.
630 		 * Otherwise just throw it away.
631 		 */
632 		if (rstr2_lsn > rstr1_lsn) {
633 			ntfs_log_debug("Using second restart page as it is more "
634 					"recent.\n");
635 			free(rstr1_ph);
636 			rstr1_ph = rstr2_ph;
637 			/* rstr1_lsn = rstr2_lsn; */
638 		} else {
639 			ntfs_log_debug("Using first restart page as it is more "
640 					"recent.\n");
641 			free(rstr2_ph);
642 		}
643 		rstr2_ph = NULL;
644 	}
645 	/* All consistency checks passed. */
646 	if (rp)
647 		*rp = rstr1_ph;
648 	else
649 		free(rstr1_ph);
650 	ntfs_log_trace("Done.\n");
651 	return TRUE;
652 err_out:
653 	free(kaddr);
654 	free(rstr1_ph);
655 	free(rstr2_ph);
656 	return FALSE;
657 }
658 
659 /**
660  * ntfs_is_logfile_clean - check in the journal if the volume is clean
661  * @log_na:	ntfs attribute of loaded journal $LogFile to check
662  * @rp:         copy of the current restart page
663  *
664  * Analyze the $LogFile journal and return TRUE if it indicates the volume was
665  * shutdown cleanly and FALSE if not.
666  *
667  * At present we only look at the two restart pages and ignore the log record
668  * pages.  This is a little bit crude in that there will be a very small number
669  * of cases where we think that a volume is dirty when in fact it is clean.
670  * This should only affect volumes that have not been shutdown cleanly but did
671  * not have any pending, non-check-pointed i/o, i.e. they were completely idle
672  * at least for the five seconds preceding the unclean shutdown.
673  *
674  * This function assumes that the $LogFile journal has already been consistency
675  * checked by a call to ntfs_check_logfile() and in particular if the $LogFile
676  * is empty this function requires that NVolLogFileEmpty() is true otherwise an
677  * empty volume will be reported as dirty.
678  */
ntfs_is_logfile_clean(ntfs_attr * log_na,RESTART_PAGE_HEADER * rp)679 BOOL ntfs_is_logfile_clean(ntfs_attr *log_na, RESTART_PAGE_HEADER *rp)
680 {
681 	RESTART_AREA *ra;
682 
683 	ntfs_log_trace("Entering.\n");
684 	/* An empty $LogFile must have been clean before it got emptied. */
685 	if (NVolLogFileEmpty(log_na->ni->vol)) {
686 		ntfs_log_trace("$LogFile is empty\n");
687 		return TRUE;
688 	}
689 	if (!rp) {
690 		ntfs_log_error("Restart page header is NULL\n");
691 		return FALSE;
692 	}
693 	if (!ntfs_is_rstr_record(rp->magic) &&
694 			!ntfs_is_chkd_record(rp->magic)) {
695 		ntfs_log_error("Restart page buffer is invalid\n");
696 		return FALSE;
697 	}
698 
699 	ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
700 	/*
701 	 * If the $LogFile has active clients, i.e. it is open, and we do not
702 	 * have the RESTART_VOLUME_IS_CLEAN bit set in the restart area flags,
703 	 * we assume there was an unclean shutdown.
704 	 */
705 	if (ra->client_in_use_list != LOGFILE_NO_CLIENT &&
706 			!(ra->flags & RESTART_VOLUME_IS_CLEAN)) {
707 		ntfs_log_error("The disk contains an unclean file system (%d, "
708 			       "%d).\n", le16_to_cpu(ra->client_in_use_list),
709 			       le16_to_cpu(ra->flags));
710 		return FALSE;
711 	}
712 	/* $LogFile indicates a clean shutdown. */
713 	ntfs_log_trace("$LogFile indicates a clean shutdown\n");
714 	return TRUE;
715 }
716 
717 /**
718  * ntfs_empty_logfile - empty the contents of the $LogFile journal
719  * @na:		ntfs attribute of journal $LogFile to empty
720  *
721  * Empty the contents of the $LogFile journal @na and return 0 on success and
722  * -1 on error.
723  *
724  * This function assumes that the $LogFile journal has already been consistency
725  * checked by a call to ntfs_check_logfile() and that ntfs_is_logfile_clean()
726  * has been used to ensure that the $LogFile is clean.
727  */
ntfs_empty_logfile(ntfs_attr * na)728 int ntfs_empty_logfile(ntfs_attr *na)
729 {
730 	s64 pos, count;
731 	char buf[NTFS_BUF_SIZE];
732 
733 	ntfs_log_trace("Entering.\n");
734 
735 	if (NVolLogFileEmpty(na->ni->vol))
736 		return 0;
737 
738 	if (!NAttrNonResident(na)) {
739 		errno = EIO;
740 		ntfs_log_perror("Resident $LogFile $DATA attribute");
741 		return -1;
742 	}
743 
744 	memset(buf, -1, NTFS_BUF_SIZE);
745 
746 	pos = 0;
747 	while ((count = na->data_size - pos) > 0) {
748 
749 		if (count > NTFS_BUF_SIZE)
750 			count = NTFS_BUF_SIZE;
751 
752 		count = ntfs_attr_pwrite(na, pos, count, buf);
753 		if (count <= 0) {
754 			ntfs_log_perror("Failed to reset $LogFile");
755 			if (count != -1)
756 				errno = EIO;
757 			return -1;
758 		}
759 		pos += count;
760 	}
761 
762 	NVolSetLogFileEmpty(na->ni->vol);
763 
764 	return 0;
765 }
766