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, 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 /* 485 * Use generic_ffs() instead of ffs() to enable the compiler to 486 * optimize log_page_size and log_page_bits into constants. 487 */ 488 log_page_bits = ffs(log_page_size) - 1; 489 size &= ~(log_page_size - 1); 490 491 /* 492 * Ensure the log file is big enough to store at least the two restart 493 * pages and the minimum number of log record pages. 494 */ 495 if (size < log_page_size * 2 || (size - log_page_size * 2) >> 496 log_page_bits < MinLogRecordPages) { 497 ntfs_log_error("$LogFile is too small.\n"); 498 return FALSE; 499 } 500 /* Allocate memory for restart page. */ 501 kaddr = ntfs_malloc(NTFS_BLOCK_SIZE); 502 if (!kaddr) 503 return FALSE; 504 /* 505 * Read through the file looking for a restart page. Since the restart 506 * page header is at the beginning of a page we only need to search at 507 * what could be the beginning of a page (for each page size) rather 508 * than scanning the whole file byte by byte. If all potential places 509 * contain empty and uninitialized records, the log file can be assumed 510 * to be empty. 511 */ 512 for (pos = 0; pos < size; pos <<= 1) { 513 /* 514 * Read first NTFS_BLOCK_SIZE bytes of potential restart page. 515 */ 516 if (ntfs_attr_pread(log_na, pos, NTFS_BLOCK_SIZE, kaddr) != 517 NTFS_BLOCK_SIZE) { 518 ntfs_log_error("Failed to read first NTFS_BLOCK_SIZE " 519 "bytes of potential restart page.\n"); 520 goto err_out; 521 } 522 523 /* 524 * A non-empty block means the logfile is not empty while an 525 * empty block after a non-empty block has been encountered 526 * means we are done. 527 */ 528 if (!ntfs_is_empty_recordp((le32*)kaddr)) 529 logfile_is_empty = FALSE; 530 else if (!logfile_is_empty) 531 break; 532 /* 533 * A log record page means there cannot be a restart page after 534 * this so no need to continue searching. 535 */ 536 if (ntfs_is_rcrd_recordp((le32*)kaddr)) 537 break; 538 /* If not a (modified by chkdsk) restart page, continue. */ 539 if (!ntfs_is_rstr_recordp((le32*)kaddr) && 540 !ntfs_is_chkd_recordp((le32*)kaddr)) { 541 if (!pos) 542 pos = NTFS_BLOCK_SIZE >> 1; 543 continue; 544 } 545 /* 546 * Check the (modified by chkdsk) restart page for consistency 547 * and get a copy of the complete multi sector transfer 548 * deprotected restart page. 549 */ 550 err = ntfs_check_and_load_restart_page(log_na, 551 (RESTART_PAGE_HEADER*)kaddr, pos, 552 !rstr1_ph ? &rstr1_ph : &rstr2_ph, 553 !rstr1_ph ? &rstr1_lsn : &rstr2_lsn); 554 if (!err) { 555 /* 556 * If we have now found the first (modified by chkdsk) 557 * restart page, continue looking for the second one. 558 */ 559 if (!pos) { 560 pos = NTFS_BLOCK_SIZE >> 1; 561 continue; 562 } 563 /* 564 * We have now found the second (modified by chkdsk) 565 * restart page, so we can stop looking. 566 */ 567 break; 568 } 569 /* 570 * Error output already done inside the function. Note, we do 571 * not abort if the restart page was invalid as we might still 572 * find a valid one further in the file. 573 */ 574 if (err != EINVAL) 575 goto err_out; 576 /* Continue looking. */ 577 if (!pos) 578 pos = NTFS_BLOCK_SIZE >> 1; 579 } 580 if (kaddr) { 581 free(kaddr); 582 kaddr = NULL; 583 } 584 if (logfile_is_empty) { 585 NVolSetLogFileEmpty(vol); 586 is_empty: 587 ntfs_log_trace("Done. ($LogFile is empty.)\n"); 588 return TRUE; 589 } 590 if (!rstr1_ph) { 591 if (rstr2_ph) 592 ntfs_log_error("BUG: rstr2_ph isn't NULL!\n"); 593 ntfs_log_error("Did not find any restart pages in " 594 "$LogFile and it was not empty.\n"); 595 return FALSE; 596 } 597 /* If both restart pages were found, use the more recent one. */ 598 if (rstr2_ph) { 599 /* 600 * If the second restart area is more recent, switch to it. 601 * Otherwise just throw it away. 602 */ 603 if (rstr2_lsn > rstr1_lsn) { 604 ntfs_log_debug("Using second restart page as it is more " 605 "recent.\n"); 606 free(rstr1_ph); 607 rstr1_ph = rstr2_ph; 608 /* rstr1_lsn = rstr2_lsn; */ 609 } else { 610 ntfs_log_debug("Using first restart page as it is more " 611 "recent.\n"); 612 free(rstr2_ph); 613 } 614 rstr2_ph = NULL; 615 } 616 /* All consistency checks passed. */ 617 if (rp) 618 *rp = rstr1_ph; 619 else 620 free(rstr1_ph); 621 ntfs_log_trace("Done.\n"); 622 return TRUE; 623 err_out: 624 free(kaddr); 625 free(rstr1_ph); 626 free(rstr2_ph); 627 return FALSE; 628 } 629 630 /** 631 * ntfs_is_logfile_clean - check in the journal if the volume is clean 632 * @log_na: ntfs attribute of loaded journal $LogFile to check 633 * @rp: copy of the current restart page 634 * 635 * Analyze the $LogFile journal and return TRUE if it indicates the volume was 636 * shutdown cleanly and FALSE if not. 637 * 638 * At present we only look at the two restart pages and ignore the log record 639 * pages. This is a little bit crude in that there will be a very small number 640 * of cases where we think that a volume is dirty when in fact it is clean. 641 * This should only affect volumes that have not been shutdown cleanly but did 642 * not have any pending, non-check-pointed i/o, i.e. they were completely idle 643 * at least for the five seconds preceding the unclean shutdown. 644 * 645 * This function assumes that the $LogFile journal has already been consistency 646 * checked by a call to ntfs_check_logfile() and in particular if the $LogFile 647 * is empty this function requires that NVolLogFileEmpty() is true otherwise an 648 * empty volume will be reported as dirty. 649 */ 650 BOOL ntfs_is_logfile_clean(ntfs_attr *log_na, RESTART_PAGE_HEADER *rp) 651 { 652 RESTART_AREA *ra; 653 654 ntfs_log_trace("Entering.\n"); 655 /* An empty $LogFile must have been clean before it got emptied. */ 656 if (NVolLogFileEmpty(log_na->ni->vol)) { 657 ntfs_log_trace("$LogFile is empty\n"); 658 return TRUE; 659 } 660 if (!rp) { 661 ntfs_log_error("Restart page header is NULL\n"); 662 return FALSE; 663 } 664 if (!ntfs_is_rstr_record(rp->magic) && 665 !ntfs_is_chkd_record(rp->magic)) { 666 ntfs_log_error("Restart page buffer is invalid\n"); 667 return FALSE; 668 } 669 670 ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset)); 671 /* 672 * If the $LogFile has active clients, i.e. it is open, and we do not 673 * have the RESTART_VOLUME_IS_CLEAN bit set in the restart area flags, 674 * we assume there was an unclean shutdown. 675 */ 676 if (ra->client_in_use_list != LOGFILE_NO_CLIENT && 677 !(ra->flags & RESTART_VOLUME_IS_CLEAN)) { 678 ntfs_log_error("The disk contains an unclean file system (%d, " 679 "%d).\n", le16_to_cpu(ra->client_in_use_list), 680 le16_to_cpu(ra->flags)); 681 return FALSE; 682 } 683 /* $LogFile indicates a clean shutdown. */ 684 ntfs_log_trace("$LogFile indicates a clean shutdown\n"); 685 return TRUE; 686 } 687 688 /** 689 * ntfs_empty_logfile - empty the contents of the $LogFile journal 690 * @na: ntfs attribute of journal $LogFile to empty 691 * 692 * Empty the contents of the $LogFile journal @na and return 0 on success and 693 * -1 on error. 694 * 695 * This function assumes that the $LogFile journal has already been consistency 696 * checked by a call to ntfs_check_logfile() and that ntfs_is_logfile_clean() 697 * has been used to ensure that the $LogFile is clean. 698 */ 699 int ntfs_empty_logfile(ntfs_attr *na) 700 { 701 s64 pos, count; 702 char buf[NTFS_BUF_SIZE]; 703 704 ntfs_log_trace("Entering.\n"); 705 706 if (NVolLogFileEmpty(na->ni->vol)) 707 return 0; 708 709 if (!NAttrNonResident(na)) { 710 errno = EIO; 711 ntfs_log_perror("Resident $LogFile $DATA attribute"); 712 return -1; 713 } 714 715 memset(buf, -1, NTFS_BUF_SIZE); 716 717 pos = 0; 718 while ((count = na->data_size - pos) > 0) { 719 720 if (count > NTFS_BUF_SIZE) 721 count = NTFS_BUF_SIZE; 722 723 count = ntfs_attr_pwrite(na, pos, count, buf); 724 if (count <= 0) { 725 ntfs_log_perror("Failed to reset $LogFile"); 726 if (count != -1) 727 errno = EIO; 728 return -1; 729 } 730 pos += count; 731 } 732 733 NVolSetLogFileEmpty(na->ni->vol); 734 735 return 0; 736 } 737