logfile.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. * logfile.c - NTFS kernel journal handling. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2002-2005 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. "by 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: [OUT] copy of the multi sector transfer deprotected restart page
  306. * @lsn: [OUT] set to the current logfile lsn on success
  307. *
  308. * Check the restart page @rp for consistency and return 0 if it is consistent
  309. * and -errno otherwise. The restart page may have been modified by chkdsk in
  310. * which case its magic is CHKD instead of RSTR.
  311. *
  312. * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
  313. * require the full restart page.
  314. *
  315. * If @wrp is not NULL, on success, *@wrp will point to a buffer containing a
  316. * copy of the complete multi sector transfer deprotected page. On failure,
  317. * *@wrp is undefined.
  318. *
  319. * Simillarly, if @lsn is not NULL, on succes *@lsn will be set to the current
  320. * logfile lsn according to this restart page. On failure, *@lsn is undefined.
  321. *
  322. * The following error codes are defined:
  323. * -EINVAL - The restart page is inconsistent.
  324. * -ENOMEM - Not enough memory to load the restart page.
  325. * -EIO - Failed to reading from $LogFile.
  326. */
  327. static int ntfs_check_and_load_restart_page(struct inode *vi,
  328. RESTART_PAGE_HEADER *rp, s64 pos, RESTART_PAGE_HEADER **wrp,
  329. LSN *lsn)
  330. {
  331. RESTART_AREA *ra;
  332. RESTART_PAGE_HEADER *trp;
  333. int size, err;
  334. ntfs_debug("Entering.");
  335. /* Check the restart page header for consistency. */
  336. if (!ntfs_check_restart_page_header(vi, rp, pos)) {
  337. /* Error output already done inside the function. */
  338. return -EINVAL;
  339. }
  340. /* Check the restart area for consistency. */
  341. if (!ntfs_check_restart_area(vi, rp)) {
  342. /* Error output already done inside the function. */
  343. return -EINVAL;
  344. }
  345. ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
  346. /*
  347. * Allocate a buffer to store the whole restart page so we can multi
  348. * sector transfer deprotect it.
  349. */
  350. trp = ntfs_malloc_nofs(le32_to_cpu(rp->system_page_size));
  351. if (!trp) {
  352. ntfs_error(vi->i_sb, "Failed to allocate memory for $LogFile "
  353. "restart page buffer.");
  354. return -ENOMEM;
  355. }
  356. /*
  357. * Read the whole of the restart page into the buffer. If it fits
  358. * completely inside @rp, just copy it from there. Otherwise map all
  359. * the required pages and copy the data from them.
  360. */
  361. size = PAGE_CACHE_SIZE - (pos & ~PAGE_CACHE_MASK);
  362. if (size >= le32_to_cpu(rp->system_page_size)) {
  363. memcpy(trp, rp, le32_to_cpu(rp->system_page_size));
  364. } else {
  365. pgoff_t idx;
  366. struct page *page;
  367. int have_read, to_read;
  368. /* First copy what we already have in @rp. */
  369. memcpy(trp, rp, size);
  370. /* Copy the remaining data one page at a time. */
  371. have_read = size;
  372. to_read = le32_to_cpu(rp->system_page_size) - size;
  373. idx = (pos + size) >> PAGE_CACHE_SHIFT;
  374. BUG_ON((pos + size) & ~PAGE_CACHE_MASK);
  375. do {
  376. page = ntfs_map_page(vi->i_mapping, idx);
  377. if (IS_ERR(page)) {
  378. ntfs_error(vi->i_sb, "Error mapping $LogFile "
  379. "page (index %lu).", idx);
  380. err = PTR_ERR(page);
  381. if (err != -EIO && err != -ENOMEM)
  382. err = -EIO;
  383. goto err_out;
  384. }
  385. size = min_t(int, to_read, PAGE_CACHE_SIZE);
  386. memcpy((u8*)trp + have_read, page_address(page), size);
  387. ntfs_unmap_page(page);
  388. have_read += size;
  389. to_read -= size;
  390. idx++;
  391. } while (to_read > 0);
  392. }
  393. /* Perform the multi sector transfer deprotection on the buffer. */
  394. if (post_read_mst_fixup((NTFS_RECORD*)trp,
  395. le32_to_cpu(rp->system_page_size))) {
  396. /*
  397. * A multi sector tranfer error was detected. We only need to
  398. * abort if the restart page contents exceed the multi sector
  399. * transfer fixup of the first sector.
  400. */
  401. if (le16_to_cpu(rp->restart_area_offset) +
  402. le16_to_cpu(ra->restart_area_length) >
  403. NTFS_BLOCK_SIZE - sizeof(u16)) {
  404. ntfs_error(vi->i_sb, "Multi sector transfer error "
  405. "detected in $LogFile restart page.");
  406. err = -EINVAL;
  407. goto err_out;
  408. }
  409. }
  410. /*
  411. * If the restart page is modified by chkdsk or there are no active
  412. * logfile clients, the logfile is consistent. Otherwise, need to
  413. * check the log client records for consistency, too.
  414. */
  415. err = 0;
  416. if (ntfs_is_rstr_record(rp->magic) &&
  417. ra->client_in_use_list != LOGFILE_NO_CLIENT) {
  418. if (!ntfs_check_log_client_array(vi, trp)) {
  419. err = -EINVAL;
  420. goto err_out;
  421. }
  422. }
  423. if (lsn) {
  424. if (ntfs_is_rstr_record(rp->magic))
  425. *lsn = sle64_to_cpu(ra->current_lsn);
  426. else /* if (ntfs_is_chkd_record(rp->magic)) */
  427. *lsn = sle64_to_cpu(rp->chkdsk_lsn);
  428. }
  429. ntfs_debug("Done.");
  430. if (wrp)
  431. *wrp = trp;
  432. else {
  433. err_out:
  434. ntfs_free(trp);
  435. }
  436. return err;
  437. }
  438. /**
  439. * ntfs_check_logfile - check the journal for consistency
  440. * @log_vi: struct inode of loaded journal $LogFile to check
  441. * @rp: [OUT] on success this is a copy of the current restart page
  442. *
  443. * Check the $LogFile journal for consistency and return TRUE if it is
  444. * consistent and FALSE if not. On success, the current restart page is
  445. * returned in *@rp. Caller must call ntfs_free(*@rp) when finished with it.
  446. *
  447. * At present we only check the two restart pages and ignore the log record
  448. * pages.
  449. *
  450. * Note that the MstProtected flag is not set on the $LogFile inode and hence
  451. * when reading pages they are not deprotected. This is because we do not know
  452. * if the $LogFile was created on a system with a different page size to ours
  453. * yet and mst deprotection would fail if our page size is smaller.
  454. */
  455. BOOL ntfs_check_logfile(struct inode *log_vi, RESTART_PAGE_HEADER **rp)
  456. {
  457. s64 size, pos;
  458. LSN rstr1_lsn, rstr2_lsn;
  459. ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
  460. struct address_space *mapping = log_vi->i_mapping;
  461. struct page *page = NULL;
  462. u8 *kaddr = NULL;
  463. RESTART_PAGE_HEADER *rstr1_ph = NULL;
  464. RESTART_PAGE_HEADER *rstr2_ph = NULL;
  465. int log_page_size, log_page_mask, err;
  466. BOOL logfile_is_empty = TRUE;
  467. u8 log_page_bits;
  468. ntfs_debug("Entering.");
  469. /* An empty $LogFile must have been clean before it got emptied. */
  470. if (NVolLogFileEmpty(vol))
  471. goto is_empty;
  472. size = i_size_read(log_vi);
  473. /* Make sure the file doesn't exceed the maximum allowed size. */
  474. if (size > MaxLogFileSize)
  475. size = MaxLogFileSize;
  476. /*
  477. * Truncate size to a multiple of the page cache size or the default
  478. * log page size if the page cache size is between the default log page
  479. * log page size if the page cache size is between the default log page
  480. * size and twice that.
  481. */
  482. if (PAGE_CACHE_SIZE >= DefaultLogPageSize && PAGE_CACHE_SIZE <=
  483. DefaultLogPageSize * 2)
  484. log_page_size = DefaultLogPageSize;
  485. else
  486. log_page_size = PAGE_CACHE_SIZE;
  487. log_page_mask = log_page_size - 1;
  488. /*
  489. * Use generic_ffs() instead of ffs() to enable the compiler to
  490. * optimize log_page_size and log_page_bits into constants.
  491. */
  492. log_page_bits = generic_ffs(log_page_size) - 1;
  493. size &= ~(s64)(log_page_size - 1);
  494. /*
  495. * Ensure the log file is big enough to store at least the two restart
  496. * pages and the minimum number of log record pages.
  497. */
  498. if (size < log_page_size * 2 || (size - log_page_size * 2) >>
  499. log_page_bits < MinLogRecordPages) {
  500. ntfs_error(vol->sb, "$LogFile is too small.");
  501. return FALSE;
  502. }
  503. /*
  504. * Read through the file looking for a restart page. Since the restart
  505. * page header is at the beginning of a page we only need to search at
  506. * what could be the beginning of a page (for each page size) rather
  507. * than scanning the whole file byte by byte. If all potential places
  508. * contain empty and uninitialzed records, the log file can be assumed
  509. * to be empty.
  510. */
  511. for (pos = 0; pos < size; pos <<= 1) {
  512. pgoff_t idx = pos >> PAGE_CACHE_SHIFT;
  513. if (!page || page->index != idx) {
  514. if (page)
  515. ntfs_unmap_page(page);
  516. page = ntfs_map_page(mapping, idx);
  517. if (IS_ERR(page)) {
  518. ntfs_error(vol->sb, "Error mapping $LogFile "
  519. "page (index %lu).", idx);
  520. goto err_out;
  521. }
  522. }
  523. kaddr = (u8*)page_address(page) + (pos & ~PAGE_CACHE_MASK);
  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_vi,
  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. ntfs_unmap_page(page);
  577. goto err_out;
  578. }
  579. /* Continue looking. */
  580. if (!pos)
  581. pos = NTFS_BLOCK_SIZE >> 1;
  582. }
  583. if (page)
  584. ntfs_unmap_page(page);
  585. if (logfile_is_empty) {
  586. NVolSetLogFileEmpty(vol);
  587. is_empty:
  588. ntfs_debug("Done. ($LogFile is empty.)");
  589. return TRUE;
  590. }
  591. if (!rstr1_ph) {
  592. BUG_ON(rstr2_ph);
  593. ntfs_error(vol->sb, "Did not find any restart pages in "
  594. "$LogFile and it was not empty.");
  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_free(rstr1_ph);
  605. rstr1_ph = rstr2_ph;
  606. /* rstr1_lsn = rstr2_lsn; */
  607. } else
  608. ntfs_free(rstr2_ph);
  609. rstr2_ph = NULL;
  610. }
  611. /* All consistency checks passed. */
  612. if (rp)
  613. *rp = rstr1_ph;
  614. else
  615. ntfs_free(rstr1_ph);
  616. ntfs_debug("Done.");
  617. return TRUE;
  618. err_out:
  619. if (rstr1_ph)
  620. ntfs_free(rstr1_ph);
  621. return FALSE;
  622. }
  623. /**
  624. * ntfs_is_logfile_clean - check in the journal if the volume is clean
  625. * @log_vi: struct inode of loaded journal $LogFile to check
  626. * @rp: copy of the current restart page
  627. *
  628. * Analyze the $LogFile journal and return TRUE if it indicates the volume was
  629. * shutdown cleanly and FALSE if not.
  630. *
  631. * At present we only look at the two restart pages and ignore the log record
  632. * pages. This is a little bit crude in that there will be a very small number
  633. * of cases where we think that a volume is dirty when in fact it is clean.
  634. * This should only affect volumes that have not been shutdown cleanly but did
  635. * not have any pending, non-check-pointed i/o, i.e. they were completely idle
  636. * at least for the five seconds preceeding the unclean shutdown.
  637. *
  638. * This function assumes that the $LogFile journal has already been consistency
  639. * checked by a call to ntfs_check_logfile() and in particular if the $LogFile
  640. * is empty this function requires that NVolLogFileEmpty() is true otherwise an
  641. * empty volume will be reported as dirty.
  642. */
  643. BOOL ntfs_is_logfile_clean(struct inode *log_vi, const RESTART_PAGE_HEADER *rp)
  644. {
  645. ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
  646. RESTART_AREA *ra;
  647. ntfs_debug("Entering.");
  648. /* An empty $LogFile must have been clean before it got emptied. */
  649. if (NVolLogFileEmpty(vol)) {
  650. ntfs_debug("Done. ($LogFile is empty.)");
  651. return TRUE;
  652. }
  653. BUG_ON(!rp);
  654. if (!ntfs_is_rstr_record(rp->magic) &&
  655. !ntfs_is_chkd_record(rp->magic)) {
  656. ntfs_error(vol->sb, "Restart page buffer is invalid. This is "
  657. "probably a bug in that the $LogFile should "
  658. "have been consistency checked before calling "
  659. "this function.");
  660. return FALSE;
  661. }
  662. ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
  663. /*
  664. * If the $LogFile has active clients, i.e. it is open, and we do not
  665. * have the RESTART_VOLUME_IS_CLEAN bit set in the restart area flags,
  666. * we assume there was an unclean shutdown.
  667. */
  668. if (ra->client_in_use_list != LOGFILE_NO_CLIENT &&
  669. !(ra->flags & RESTART_VOLUME_IS_CLEAN)) {
  670. ntfs_debug("Done. $LogFile indicates a dirty shutdown.");
  671. return FALSE;
  672. }
  673. /* $LogFile indicates a clean shutdown. */
  674. ntfs_debug("Done. $LogFile indicates a clean shutdown.");
  675. return TRUE;
  676. }
  677. /**
  678. * ntfs_empty_logfile - empty the contents of the $LogFile journal
  679. * @log_vi: struct inode of loaded journal $LogFile to empty
  680. *
  681. * Empty the contents of the $LogFile journal @log_vi and return TRUE on
  682. * success and FALSE on error.
  683. *
  684. * This function assumes that the $LogFile journal has already been consistency
  685. * checked by a call to ntfs_check_logfile() and that ntfs_is_logfile_clean()
  686. * has been used to ensure that the $LogFile is clean.
  687. */
  688. BOOL ntfs_empty_logfile(struct inode *log_vi)
  689. {
  690. ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
  691. ntfs_debug("Entering.");
  692. if (!NVolLogFileEmpty(vol)) {
  693. int err;
  694. err = ntfs_attr_set(NTFS_I(log_vi), 0, i_size_read(log_vi),
  695. 0xff);
  696. if (unlikely(err)) {
  697. ntfs_error(vol->sb, "Failed to fill $LogFile with "
  698. "0xff bytes (error code %i).", err);
  699. return FALSE;
  700. }
  701. /* Set the flag so we do not have to do it again on remount. */
  702. NVolSetLogFileEmpty(vol);
  703. }
  704. ntfs_debug("Done.");
  705. return TRUE;
  706. }
  707. #endif /* NTFS_RW */