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