logfile.c 28 KB

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