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