Blame view

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