logfile.c 23 KB

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