logfile.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * logfile.c - NTFS kernel journal handling. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2002-2007 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 <linux/log2.h>
  28. #include <linux/bio.h>
  29. #include "attrib.h"
  30. #include "aops.h"
  31. #include "debug.h"
  32. #include "logfile.h"
  33. #include "malloc.h"
  34. #include "volume.h"
  35. #include "ntfs.h"
  36. /**
  37. * ntfs_check_restart_page_header - check the page header for consistency
  38. * @vi: $LogFile inode to which the restart page header belongs
  39. * @rp: restart page header to check
  40. * @pos: position in @vi at which the restart page header resides
  41. *
  42. * Check the restart page header @rp for consistency and return 'true' if it is
  43. * consistent and 'false' otherwise.
  44. *
  45. * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
  46. * require the full restart page.
  47. */
  48. static bool ntfs_check_restart_page_header(struct inode *vi,
  49. RESTART_PAGE_HEADER *rp, s64 pos)
  50. {
  51. u32 logfile_system_page_size, logfile_log_page_size;
  52. u16 ra_ofs, usa_count, usa_ofs, usa_end = 0;
  53. bool have_usa = true;
  54. ntfs_debug("Entering.");
  55. /*
  56. * If the system or log page sizes are smaller than the ntfs block size
  57. * or either is not a power of 2 we cannot handle this log file.
  58. */
  59. logfile_system_page_size = le32_to_cpu(rp->system_page_size);
  60. logfile_log_page_size = le32_to_cpu(rp->log_page_size);
  61. if (logfile_system_page_size < NTFS_BLOCK_SIZE ||
  62. logfile_log_page_size < NTFS_BLOCK_SIZE ||
  63. logfile_system_page_size &
  64. (logfile_system_page_size - 1) ||
  65. !is_power_of_2(logfile_log_page_size)) {
  66. ntfs_error(vi->i_sb, "$LogFile uses unsupported page size.");
  67. return false;
  68. }
  69. /*
  70. * We must be either at !pos (1st restart page) or at pos = system page
  71. * size (2nd restart page).
  72. */
  73. if (pos && pos != logfile_system_page_size) {
  74. ntfs_error(vi->i_sb, "Found restart area in incorrect "
  75. "position in $LogFile.");
  76. return false;
  77. }
  78. /* We only know how to handle version 1.1. */
  79. if (sle16_to_cpu(rp->major_ver) != 1 ||
  80. sle16_to_cpu(rp->minor_ver) != 1) {
  81. ntfs_error(vi->i_sb, "$LogFile version %i.%i is not "
  82. "supported. (This driver supports version "
  83. "1.1 only.)", (int)sle16_to_cpu(rp->major_ver),
  84. (int)sle16_to_cpu(rp->minor_ver));
  85. return false;
  86. }
  87. /*
  88. * If chkdsk has been run the restart page may not be protected by an
  89. * update sequence array.
  90. */
  91. if (ntfs_is_chkd_record(rp->magic) && !le16_to_cpu(rp->usa_count)) {
  92. have_usa = false;
  93. goto skip_usa_checks;
  94. }
  95. /* Verify the size of the update sequence array. */
  96. usa_count = 1 + (logfile_system_page_size >> NTFS_BLOCK_SIZE_BITS);
  97. if (usa_count != le16_to_cpu(rp->usa_count)) {
  98. ntfs_error(vi->i_sb, "$LogFile restart page specifies "
  99. "inconsistent update sequence array count.");
  100. return false;
  101. }
  102. /* Verify the position of the update sequence array. */
  103. usa_ofs = le16_to_cpu(rp->usa_ofs);
  104. usa_end = usa_ofs + usa_count * sizeof(u16);
  105. if (usa_ofs < sizeof(RESTART_PAGE_HEADER) ||
  106. usa_end > NTFS_BLOCK_SIZE - sizeof(u16)) {
  107. ntfs_error(vi->i_sb, "$LogFile restart page specifies "
  108. "inconsistent update sequence array offset.");
  109. return false;
  110. }
  111. skip_usa_checks:
  112. /*
  113. * Verify the position of the restart area. It must be:
  114. * - aligned to 8-byte boundary,
  115. * - after the update sequence array, and
  116. * - within the system page size.
  117. */
  118. ra_ofs = le16_to_cpu(rp->restart_area_offset);
  119. if (ra_ofs & 7 || (have_usa ? ra_ofs < usa_end :
  120. ra_ofs < sizeof(RESTART_PAGE_HEADER)) ||
  121. ra_ofs > logfile_system_page_size) {
  122. ntfs_error(vi->i_sb, "$LogFile restart page specifies "
  123. "inconsistent restart area offset.");
  124. return false;
  125. }
  126. /*
  127. * Only restart pages modified by chkdsk are allowed to have chkdsk_lsn
  128. * set.
  129. */
  130. if (!ntfs_is_chkd_record(rp->magic) && sle64_to_cpu(rp->chkdsk_lsn)) {
  131. ntfs_error(vi->i_sb, "$LogFile restart page is not modified "
  132. "by chkdsk but a chkdsk LSN is specified.");
  133. return false;
  134. }
  135. ntfs_debug("Done.");
  136. return true;
  137. }
  138. /**
  139. * ntfs_check_restart_area - check the restart area for consistency
  140. * @vi: $LogFile inode to which the restart page belongs
  141. * @rp: restart page whose restart area to check
  142. *
  143. * Check the restart area of the restart page @rp for consistency and return
  144. * 'true' if it is consistent and 'false' otherwise.
  145. *
  146. * This function assumes that the restart page header has already been
  147. * consistency checked.
  148. *
  149. * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
  150. * require the full restart page.
  151. */
  152. static bool ntfs_check_restart_area(struct inode *vi, RESTART_PAGE_HEADER *rp)
  153. {
  154. u64 file_size;
  155. RESTART_AREA *ra;
  156. u16 ra_ofs, ra_len, ca_ofs;
  157. u8 fs_bits;
  158. ntfs_debug("Entering.");
  159. ra_ofs = le16_to_cpu(rp->restart_area_offset);
  160. ra = (RESTART_AREA*)((u8*)rp + ra_ofs);
  161. /*
  162. * Everything before ra->file_size must be before the first word
  163. * protected by an update sequence number. This ensures that it is
  164. * safe to access ra->client_array_offset.
  165. */
  166. if (ra_ofs + offsetof(RESTART_AREA, file_size) >
  167. NTFS_BLOCK_SIZE - sizeof(u16)) {
  168. ntfs_error(vi->i_sb, "$LogFile restart area specifies "
  169. "inconsistent file offset.");
  170. return false;
  171. }
  172. /*
  173. * Now that we can access ra->client_array_offset, make sure everything
  174. * up to the log client array is before the first word protected by an
  175. * update sequence number. This ensures we can access all of the
  176. * restart area elements safely. Also, the client array offset must be
  177. * aligned to an 8-byte boundary.
  178. */
  179. ca_ofs = le16_to_cpu(ra->client_array_offset);
  180. if (((ca_ofs + 7) & ~7) != ca_ofs ||
  181. ra_ofs + ca_ofs > NTFS_BLOCK_SIZE - sizeof(u16)) {
  182. ntfs_error(vi->i_sb, "$LogFile restart area specifies "
  183. "inconsistent client array offset.");
  184. return false;
  185. }
  186. /*
  187. * The restart area must end within the system page size both when
  188. * calculated manually and as specified by ra->restart_area_length.
  189. * Also, the calculated length must not exceed the specified length.
  190. */
  191. ra_len = ca_ofs + le16_to_cpu(ra->log_clients) *
  192. sizeof(LOG_CLIENT_RECORD);
  193. if (ra_ofs + ra_len > le32_to_cpu(rp->system_page_size) ||
  194. ra_ofs + le16_to_cpu(ra->restart_area_length) >
  195. le32_to_cpu(rp->system_page_size) ||
  196. ra_len > le16_to_cpu(ra->restart_area_length)) {
  197. ntfs_error(vi->i_sb, "$LogFile restart area is out of bounds "
  198. "of the system page size specified by the "
  199. "restart page header and/or the specified "
  200. "restart area length is inconsistent.");
  201. return false;
  202. }
  203. /*
  204. * The ra->client_free_list and ra->client_in_use_list must be either
  205. * LOGFILE_NO_CLIENT or less than ra->log_clients or they are
  206. * overflowing the client array.
  207. */
  208. if ((ra->client_free_list != LOGFILE_NO_CLIENT &&
  209. le16_to_cpu(ra->client_free_list) >=
  210. le16_to_cpu(ra->log_clients)) ||
  211. (ra->client_in_use_list != LOGFILE_NO_CLIENT &&
  212. le16_to_cpu(ra->client_in_use_list) >=
  213. le16_to_cpu(ra->log_clients))) {
  214. ntfs_error(vi->i_sb, "$LogFile restart area specifies "
  215. "overflowing client free and/or in use lists.");
  216. return false;
  217. }
  218. /*
  219. * Check ra->seq_number_bits against ra->file_size for consistency.
  220. * We cannot just use ffs() because the file size is not a power of 2.
  221. */
  222. file_size = (u64)sle64_to_cpu(ra->file_size);
  223. fs_bits = 0;
  224. while (file_size) {
  225. file_size >>= 1;
  226. fs_bits++;
  227. }
  228. if (le32_to_cpu(ra->seq_number_bits) != 67 - fs_bits) {
  229. ntfs_error(vi->i_sb, "$LogFile restart area specifies "
  230. "inconsistent sequence number bits.");
  231. return false;
  232. }
  233. /* The log record header length must be a multiple of 8. */
  234. if (((le16_to_cpu(ra->log_record_header_length) + 7) & ~7) !=
  235. le16_to_cpu(ra->log_record_header_length)) {
  236. ntfs_error(vi->i_sb, "$LogFile restart area specifies "
  237. "inconsistent log record header length.");
  238. return false;
  239. }
  240. /* Dito for the log page data offset. */
  241. if (((le16_to_cpu(ra->log_page_data_offset) + 7) & ~7) !=
  242. le16_to_cpu(ra->log_page_data_offset)) {
  243. ntfs_error(vi->i_sb, "$LogFile restart area specifies "
  244. "inconsistent log page data offset.");
  245. return false;
  246. }
  247. ntfs_debug("Done.");
  248. return true;
  249. }
  250. /**
  251. * ntfs_check_log_client_array - check the log client array for consistency
  252. * @vi: $LogFile inode to which the restart page belongs
  253. * @rp: restart page whose log client array to check
  254. *
  255. * Check the log client array of the restart page @rp for consistency and
  256. * return 'true' if it is consistent and 'false' otherwise.
  257. *
  258. * This function assumes that the restart page header and the restart area have
  259. * already been consistency checked.
  260. *
  261. * Unlike ntfs_check_restart_page_header() and ntfs_check_restart_area(), this
  262. * function needs @rp->system_page_size bytes in @rp, i.e. it requires the full
  263. * restart page and the page must be multi sector transfer deprotected.
  264. */
  265. static bool ntfs_check_log_client_array(struct inode *vi,
  266. RESTART_PAGE_HEADER *rp)
  267. {
  268. RESTART_AREA *ra;
  269. LOG_CLIENT_RECORD *ca, *cr;
  270. u16 nr_clients, idx;
  271. bool in_free_list, idx_is_first;
  272. ntfs_debug("Entering.");
  273. ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
  274. ca = (LOG_CLIENT_RECORD*)((u8*)ra +
  275. le16_to_cpu(ra->client_array_offset));
  276. /*
  277. * Check the ra->client_free_list first and then check the
  278. * ra->client_in_use_list. Check each of the log client records in
  279. * each of the lists and check that the array does not overflow the
  280. * ra->log_clients value. Also keep track of the number of records
  281. * visited as there cannot be more than ra->log_clients records and
  282. * that way we detect eventual loops in within a list.
  283. */
  284. nr_clients = le16_to_cpu(ra->log_clients);
  285. idx = le16_to_cpu(ra->client_free_list);
  286. in_free_list = true;
  287. check_list:
  288. for (idx_is_first = true; idx != LOGFILE_NO_CLIENT_CPU; nr_clients--,
  289. idx = le16_to_cpu(cr->next_client)) {
  290. if (!nr_clients || idx >= le16_to_cpu(ra->log_clients))
  291. goto err_out;
  292. /* Set @cr to the current log client record. */
  293. cr = ca + idx;
  294. /* The first log client record must not have a prev_client. */
  295. if (idx_is_first) {
  296. if (cr->prev_client != LOGFILE_NO_CLIENT)
  297. goto err_out;
  298. idx_is_first = false;
  299. }
  300. }
  301. /* Switch to and check the in use list if we just did the free list. */
  302. if (in_free_list) {
  303. in_free_list = false;
  304. idx = le16_to_cpu(ra->client_in_use_list);
  305. goto check_list;
  306. }
  307. ntfs_debug("Done.");
  308. return true;
  309. err_out:
  310. ntfs_error(vi->i_sb, "$LogFile log client array is corrupt.");
  311. return false;
  312. }
  313. /**
  314. * ntfs_check_and_load_restart_page - check the restart page for consistency
  315. * @vi: $LogFile inode to which the restart page belongs
  316. * @rp: restart page to check
  317. * @pos: position in @vi at which the restart page resides
  318. * @wrp: [OUT] copy of the multi sector transfer deprotected restart page
  319. * @lsn: [OUT] set to the current logfile lsn on success
  320. *
  321. * Check the restart page @rp for consistency and return 0 if it is consistent
  322. * and -errno otherwise. The restart page may have been modified by chkdsk in
  323. * which case its magic is CHKD instead of RSTR.
  324. *
  325. * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not
  326. * require the full restart page.
  327. *
  328. * If @wrp is not NULL, on success, *@wrp will point to a buffer containing a
  329. * copy of the complete multi sector transfer deprotected page. On failure,
  330. * *@wrp is undefined.
  331. *
  332. * Simillarly, if @lsn is not NULL, on success *@lsn will be set to the current
  333. * logfile lsn according to this restart page. On failure, *@lsn is undefined.
  334. *
  335. * The following error codes are defined:
  336. * -EINVAL - The restart page is inconsistent.
  337. * -ENOMEM - Not enough memory to load the restart page.
  338. * -EIO - Failed to reading from $LogFile.
  339. */
  340. static int ntfs_check_and_load_restart_page(struct inode *vi,
  341. RESTART_PAGE_HEADER *rp, s64 pos, RESTART_PAGE_HEADER **wrp,
  342. LSN *lsn)
  343. {
  344. RESTART_AREA *ra;
  345. RESTART_PAGE_HEADER *trp;
  346. int size, err;
  347. ntfs_debug("Entering.");
  348. /* Check the restart page header for consistency. */
  349. if (!ntfs_check_restart_page_header(vi, rp, pos)) {
  350. /* Error output already done inside the function. */
  351. return -EINVAL;
  352. }
  353. /* Check the restart area for consistency. */
  354. if (!ntfs_check_restart_area(vi, rp)) {
  355. /* Error output already done inside the function. */
  356. return -EINVAL;
  357. }
  358. ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
  359. /*
  360. * Allocate a buffer to store the whole restart page so we can multi
  361. * sector transfer deprotect it.
  362. */
  363. trp = ntfs_malloc_nofs(le32_to_cpu(rp->system_page_size));
  364. if (!trp) {
  365. ntfs_error(vi->i_sb, "Failed to allocate memory for $LogFile "
  366. "restart page buffer.");
  367. return -ENOMEM;
  368. }
  369. /*
  370. * Read the whole of the restart page into the buffer. If it fits
  371. * completely inside @rp, just copy it from there. Otherwise map all
  372. * the required pages and copy the data from them.
  373. */
  374. size = PAGE_SIZE - (pos & ~PAGE_MASK);
  375. if (size >= le32_to_cpu(rp->system_page_size)) {
  376. memcpy(trp, rp, le32_to_cpu(rp->system_page_size));
  377. } else {
  378. pgoff_t idx;
  379. struct page *page;
  380. int have_read, to_read;
  381. /* First copy what we already have in @rp. */
  382. memcpy(trp, rp, size);
  383. /* Copy the remaining data one page at a time. */
  384. have_read = size;
  385. to_read = le32_to_cpu(rp->system_page_size) - size;
  386. idx = (pos + size) >> PAGE_SHIFT;
  387. BUG_ON((pos + size) & ~PAGE_MASK);
  388. do {
  389. page = ntfs_map_page(vi->i_mapping, idx);
  390. if (IS_ERR(page)) {
  391. ntfs_error(vi->i_sb, "Error mapping $LogFile "
  392. "page (index %lu).", idx);
  393. err = PTR_ERR(page);
  394. if (err != -EIO && err != -ENOMEM)
  395. err = -EIO;
  396. goto err_out;
  397. }
  398. size = min_t(int, to_read, PAGE_SIZE);
  399. memcpy((u8*)trp + have_read, page_address(page), size);
  400. ntfs_unmap_page(page);
  401. have_read += size;
  402. to_read -= size;
  403. idx++;
  404. } while (to_read > 0);
  405. }
  406. /*
  407. * Perform the multi sector transfer deprotection on the buffer if the
  408. * restart page is protected.
  409. */
  410. if ((!ntfs_is_chkd_record(trp->magic) || le16_to_cpu(trp->usa_count))
  411. && post_read_mst_fixup((NTFS_RECORD*)trp,
  412. le32_to_cpu(rp->system_page_size))) {
  413. /*
  414. * A multi sector tranfer error was detected. We only need to
  415. * abort if the restart page contents exceed the multi sector
  416. * transfer fixup of the first sector.
  417. */
  418. if (le16_to_cpu(rp->restart_area_offset) +
  419. le16_to_cpu(ra->restart_area_length) >
  420. NTFS_BLOCK_SIZE - sizeof(u16)) {
  421. ntfs_error(vi->i_sb, "Multi sector transfer error "
  422. "detected in $LogFile restart page.");
  423. err = -EINVAL;
  424. goto err_out;
  425. }
  426. }
  427. /*
  428. * If the restart page is modified by chkdsk or there are no active
  429. * logfile clients, the logfile is consistent. Otherwise, need to
  430. * check the log client records for consistency, too.
  431. */
  432. err = 0;
  433. if (ntfs_is_rstr_record(rp->magic) &&
  434. ra->client_in_use_list != LOGFILE_NO_CLIENT) {
  435. if (!ntfs_check_log_client_array(vi, trp)) {
  436. err = -EINVAL;
  437. goto err_out;
  438. }
  439. }
  440. if (lsn) {
  441. if (ntfs_is_rstr_record(rp->magic))
  442. *lsn = sle64_to_cpu(ra->current_lsn);
  443. else /* if (ntfs_is_chkd_record(rp->magic)) */
  444. *lsn = sle64_to_cpu(rp->chkdsk_lsn);
  445. }
  446. ntfs_debug("Done.");
  447. if (wrp)
  448. *wrp = trp;
  449. else {
  450. err_out:
  451. ntfs_free(trp);
  452. }
  453. return err;
  454. }
  455. /**
  456. * ntfs_check_logfile - check the journal for consistency
  457. * @log_vi: struct inode 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. */
  472. bool ntfs_check_logfile(struct inode *log_vi, RESTART_PAGE_HEADER **rp)
  473. {
  474. s64 size, pos;
  475. LSN rstr1_lsn, rstr2_lsn;
  476. ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
  477. struct address_space *mapping = log_vi->i_mapping;
  478. struct page *page = NULL;
  479. u8 *kaddr = NULL;
  480. RESTART_PAGE_HEADER *rstr1_ph = NULL;
  481. RESTART_PAGE_HEADER *rstr2_ph = NULL;
  482. int log_page_size, log_page_mask, err;
  483. bool logfile_is_empty = true;
  484. u8 log_page_bits;
  485. ntfs_debug("Entering.");
  486. /* An empty $LogFile must have been clean before it got emptied. */
  487. if (NVolLogFileEmpty(vol))
  488. goto is_empty;
  489. size = i_size_read(log_vi);
  490. /* Make sure the file doesn't exceed the maximum allowed size. */
  491. if (size > MaxLogFileSize)
  492. size = MaxLogFileSize;
  493. /*
  494. * Truncate size to a multiple of the page cache size or the default
  495. * log page size if the page cache size is between the default log page
  496. * log page size if the page cache size is between the default log page
  497. * size and twice that.
  498. */
  499. if (PAGE_SIZE >= DefaultLogPageSize && PAGE_SIZE <=
  500. DefaultLogPageSize * 2)
  501. log_page_size = DefaultLogPageSize;
  502. else
  503. log_page_size = PAGE_SIZE;
  504. log_page_mask = log_page_size - 1;
  505. /*
  506. * Use ntfs_ffs() instead of ffs() to enable the compiler to
  507. * optimize log_page_size and log_page_bits into constants.
  508. */
  509. log_page_bits = ntfs_ffs(log_page_size) - 1;
  510. size &= ~(s64)(log_page_size - 1);
  511. /*
  512. * Ensure the log file is big enough to store at least the two restart
  513. * pages and the minimum number of log record pages.
  514. */
  515. if (size < log_page_size * 2 || (size - log_page_size * 2) >>
  516. log_page_bits < MinLogRecordPages) {
  517. ntfs_error(vol->sb, "$LogFile is too small.");
  518. return false;
  519. }
  520. /*
  521. * Read through the file looking for a restart page. Since the restart
  522. * page header is at the beginning of a page we only need to search at
  523. * what could be the beginning of a page (for each page size) rather
  524. * than scanning the whole file byte by byte. If all potential places
  525. * contain empty and uninitialzed records, the log file can be assumed
  526. * to be empty.
  527. */
  528. for (pos = 0; pos < size; pos <<= 1) {
  529. pgoff_t idx = pos >> PAGE_SHIFT;
  530. if (!page || page->index != idx) {
  531. if (page)
  532. ntfs_unmap_page(page);
  533. page = ntfs_map_page(mapping, idx);
  534. if (IS_ERR(page)) {
  535. ntfs_error(vol->sb, "Error mapping $LogFile "
  536. "page (index %lu).", idx);
  537. goto err_out;
  538. }
  539. }
  540. kaddr = (u8*)page_address(page) + (pos & ~PAGE_MASK);
  541. /*
  542. * A non-empty block means the logfile is not empty while an
  543. * empty block after a non-empty block has been encountered
  544. * means we are done.
  545. */
  546. if (!ntfs_is_empty_recordp((le32*)kaddr))
  547. logfile_is_empty = false;
  548. else if (!logfile_is_empty)
  549. break;
  550. /*
  551. * A log record page means there cannot be a restart page after
  552. * this so no need to continue searching.
  553. */
  554. if (ntfs_is_rcrd_recordp((le32*)kaddr))
  555. break;
  556. /* If not a (modified by chkdsk) restart page, continue. */
  557. if (!ntfs_is_rstr_recordp((le32*)kaddr) &&
  558. !ntfs_is_chkd_recordp((le32*)kaddr)) {
  559. if (!pos)
  560. pos = NTFS_BLOCK_SIZE >> 1;
  561. continue;
  562. }
  563. /*
  564. * Check the (modified by chkdsk) restart page for consistency
  565. * and get a copy of the complete multi sector transfer
  566. * deprotected restart page.
  567. */
  568. err = ntfs_check_and_load_restart_page(log_vi,
  569. (RESTART_PAGE_HEADER*)kaddr, pos,
  570. !rstr1_ph ? &rstr1_ph : &rstr2_ph,
  571. !rstr1_ph ? &rstr1_lsn : &rstr2_lsn);
  572. if (!err) {
  573. /*
  574. * If we have now found the first (modified by chkdsk)
  575. * restart page, continue looking for the second one.
  576. */
  577. if (!pos) {
  578. pos = NTFS_BLOCK_SIZE >> 1;
  579. continue;
  580. }
  581. /*
  582. * We have now found the second (modified by chkdsk)
  583. * restart page, so we can stop looking.
  584. */
  585. break;
  586. }
  587. /*
  588. * Error output already done inside the function. Note, we do
  589. * not abort if the restart page was invalid as we might still
  590. * find a valid one further in the file.
  591. */
  592. if (err != -EINVAL) {
  593. ntfs_unmap_page(page);
  594. goto err_out;
  595. }
  596. /* Continue looking. */
  597. if (!pos)
  598. pos = NTFS_BLOCK_SIZE >> 1;
  599. }
  600. if (page)
  601. ntfs_unmap_page(page);
  602. if (logfile_is_empty) {
  603. NVolSetLogFileEmpty(vol);
  604. is_empty:
  605. ntfs_debug("Done. ($LogFile is empty.)");
  606. return true;
  607. }
  608. if (!rstr1_ph) {
  609. BUG_ON(rstr2_ph);
  610. ntfs_error(vol->sb, "Did not find any restart pages in "
  611. "$LogFile and it was not empty.");
  612. return false;
  613. }
  614. /* If both restart pages were found, use the more recent one. */
  615. if (rstr2_ph) {
  616. /*
  617. * If the second restart area is more recent, switch to it.
  618. * Otherwise just throw it away.
  619. */
  620. if (rstr2_lsn > rstr1_lsn) {
  621. ntfs_debug("Using second restart page as it is more "
  622. "recent.");
  623. ntfs_free(rstr1_ph);
  624. rstr1_ph = rstr2_ph;
  625. /* rstr1_lsn = rstr2_lsn; */
  626. } else {
  627. ntfs_debug("Using first restart page as it is more "
  628. "recent.");
  629. ntfs_free(rstr2_ph);
  630. }
  631. rstr2_ph = NULL;
  632. }
  633. /* All consistency checks passed. */
  634. if (rp)
  635. *rp = rstr1_ph;
  636. else
  637. ntfs_free(rstr1_ph);
  638. ntfs_debug("Done.");
  639. return true;
  640. err_out:
  641. if (rstr1_ph)
  642. ntfs_free(rstr1_ph);
  643. return false;
  644. }
  645. /**
  646. * ntfs_is_logfile_clean - check in the journal if the volume is clean
  647. * @log_vi: struct inode of loaded journal $LogFile to check
  648. * @rp: copy of the current restart page
  649. *
  650. * Analyze the $LogFile journal and return 'true' if it indicates the volume was
  651. * shutdown cleanly and 'false' if not.
  652. *
  653. * At present we only look at the two restart pages and ignore the log record
  654. * pages. This is a little bit crude in that there will be a very small number
  655. * of cases where we think that a volume is dirty when in fact it is clean.
  656. * This should only affect volumes that have not been shutdown cleanly but did
  657. * not have any pending, non-check-pointed i/o, i.e. they were completely idle
  658. * at least for the five seconds preceding the unclean shutdown.
  659. *
  660. * This function assumes that the $LogFile journal has already been consistency
  661. * checked by a call to ntfs_check_logfile() and in particular if the $LogFile
  662. * is empty this function requires that NVolLogFileEmpty() is true otherwise an
  663. * empty volume will be reported as dirty.
  664. */
  665. bool ntfs_is_logfile_clean(struct inode *log_vi, const RESTART_PAGE_HEADER *rp)
  666. {
  667. ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
  668. RESTART_AREA *ra;
  669. ntfs_debug("Entering.");
  670. /* An empty $LogFile must have been clean before it got emptied. */
  671. if (NVolLogFileEmpty(vol)) {
  672. ntfs_debug("Done. ($LogFile is empty.)");
  673. return true;
  674. }
  675. BUG_ON(!rp);
  676. if (!ntfs_is_rstr_record(rp->magic) &&
  677. !ntfs_is_chkd_record(rp->magic)) {
  678. ntfs_error(vol->sb, "Restart page buffer is invalid. This is "
  679. "probably a bug in that the $LogFile should "
  680. "have been consistency checked before calling "
  681. "this function.");
  682. return false;
  683. }
  684. ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
  685. /*
  686. * If the $LogFile has active clients, i.e. it is open, and we do not
  687. * have the RESTART_VOLUME_IS_CLEAN bit set in the restart area flags,
  688. * we assume there was an unclean shutdown.
  689. */
  690. if (ra->client_in_use_list != LOGFILE_NO_CLIENT &&
  691. !(ra->flags & RESTART_VOLUME_IS_CLEAN)) {
  692. ntfs_debug("Done. $LogFile indicates a dirty shutdown.");
  693. return false;
  694. }
  695. /* $LogFile indicates a clean shutdown. */
  696. ntfs_debug("Done. $LogFile indicates a clean shutdown.");
  697. return true;
  698. }
  699. /**
  700. * ntfs_empty_logfile - empty the contents of the $LogFile journal
  701. * @log_vi: struct inode of loaded journal $LogFile to empty
  702. *
  703. * Empty the contents of the $LogFile journal @log_vi and return 'true' on
  704. * success and 'false' on error.
  705. *
  706. * This function assumes that the $LogFile journal has already been consistency
  707. * checked by a call to ntfs_check_logfile() and that ntfs_is_logfile_clean()
  708. * has been used to ensure that the $LogFile is clean.
  709. */
  710. bool ntfs_empty_logfile(struct inode *log_vi)
  711. {
  712. VCN vcn, end_vcn;
  713. ntfs_inode *log_ni = NTFS_I(log_vi);
  714. ntfs_volume *vol = log_ni->vol;
  715. struct super_block *sb = vol->sb;
  716. runlist_element *rl;
  717. unsigned long flags;
  718. unsigned block_size, block_size_bits;
  719. int err;
  720. bool should_wait = true;
  721. ntfs_debug("Entering.");
  722. if (NVolLogFileEmpty(vol)) {
  723. ntfs_debug("Done.");
  724. return true;
  725. }
  726. /*
  727. * We cannot use ntfs_attr_set() because we may be still in the middle
  728. * of a mount operation. Thus we do the emptying by hand by first
  729. * zapping the page cache pages for the $LogFile/$DATA attribute and
  730. * then emptying each of the buffers in each of the clusters specified
  731. * by the runlist by hand.
  732. */
  733. block_size = sb->s_blocksize;
  734. block_size_bits = sb->s_blocksize_bits;
  735. vcn = 0;
  736. read_lock_irqsave(&log_ni->size_lock, flags);
  737. end_vcn = (log_ni->initialized_size + vol->cluster_size_mask) >>
  738. vol->cluster_size_bits;
  739. read_unlock_irqrestore(&log_ni->size_lock, flags);
  740. truncate_inode_pages(log_vi->i_mapping, 0);
  741. down_write(&log_ni->runlist.lock);
  742. rl = log_ni->runlist.rl;
  743. if (unlikely(!rl || vcn < rl->vcn || !rl->length)) {
  744. map_vcn:
  745. err = ntfs_map_runlist_nolock(log_ni, vcn, NULL);
  746. if (err) {
  747. ntfs_error(sb, "Failed to map runlist fragment (error "
  748. "%d).", -err);
  749. goto err;
  750. }
  751. rl = log_ni->runlist.rl;
  752. BUG_ON(!rl || vcn < rl->vcn || !rl->length);
  753. }
  754. /* Seek to the runlist element containing @vcn. */
  755. while (rl->length && vcn >= rl[1].vcn)
  756. rl++;
  757. do {
  758. LCN lcn;
  759. sector_t block, end_block;
  760. s64 len;
  761. /*
  762. * If this run is not mapped map it now and start again as the
  763. * runlist will have been updated.
  764. */
  765. lcn = rl->lcn;
  766. if (unlikely(lcn == LCN_RL_NOT_MAPPED)) {
  767. vcn = rl->vcn;
  768. goto map_vcn;
  769. }
  770. /* If this run is not valid abort with an error. */
  771. if (unlikely(!rl->length || lcn < LCN_HOLE))
  772. goto rl_err;
  773. /* Skip holes. */
  774. if (lcn == LCN_HOLE)
  775. continue;
  776. block = lcn << vol->cluster_size_bits >> block_size_bits;
  777. len = rl->length;
  778. if (rl[1].vcn > end_vcn)
  779. len = end_vcn - rl->vcn;
  780. end_block = (lcn + len) << vol->cluster_size_bits >>
  781. block_size_bits;
  782. /* Iterate over the blocks in the run and empty them. */
  783. do {
  784. struct buffer_head *bh;
  785. /* Obtain the buffer, possibly not uptodate. */
  786. bh = sb_getblk(sb, block);
  787. BUG_ON(!bh);
  788. /* Setup buffer i/o submission. */
  789. lock_buffer(bh);
  790. bh->b_end_io = end_buffer_write_sync;
  791. get_bh(bh);
  792. /* Set the entire contents of the buffer to 0xff. */
  793. memset(bh->b_data, -1, block_size);
  794. if (!buffer_uptodate(bh))
  795. set_buffer_uptodate(bh);
  796. if (buffer_dirty(bh))
  797. clear_buffer_dirty(bh);
  798. /*
  799. * Submit the buffer and wait for i/o to complete but
  800. * only for the first buffer so we do not miss really
  801. * serious i/o errors. Once the first buffer has
  802. * completed ignore errors afterwards as we can assume
  803. * that if one buffer worked all of them will work.
  804. */
  805. submit_bh(REQ_OP_WRITE, 0, bh);
  806. if (should_wait) {
  807. should_wait = false;
  808. wait_on_buffer(bh);
  809. if (unlikely(!buffer_uptodate(bh)))
  810. goto io_err;
  811. }
  812. brelse(bh);
  813. } while (++block < end_block);
  814. } while ((++rl)->vcn < end_vcn);
  815. up_write(&log_ni->runlist.lock);
  816. /*
  817. * Zap the pages again just in case any got instantiated whilst we were
  818. * emptying the blocks by hand. FIXME: We may not have completed
  819. * writing to all the buffer heads yet so this may happen too early.
  820. * We really should use a kernel thread to do the emptying
  821. * asynchronously and then we can also set the volume dirty and output
  822. * an error message if emptying should fail.
  823. */
  824. truncate_inode_pages(log_vi->i_mapping, 0);
  825. /* Set the flag so we do not have to do it again on remount. */
  826. NVolSetLogFileEmpty(vol);
  827. ntfs_debug("Done.");
  828. return true;
  829. io_err:
  830. ntfs_error(sb, "Failed to write buffer. Unmount and run chkdsk.");
  831. goto dirty_err;
  832. rl_err:
  833. ntfs_error(sb, "Runlist is corrupt. Unmount and run chkdsk.");
  834. dirty_err:
  835. NVolSetErrors(vol);
  836. err = -EIO;
  837. err:
  838. up_write(&log_ni->runlist.lock);
  839. ntfs_error(sb, "Failed to fill $LogFile with 0xff bytes (error %d).",
  840. -err);
  841. return false;
  842. }
  843. #endif /* NTFS_RW */