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