logfile.c 28 KB

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