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