logfile.c 27 KB

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