logfile.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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, 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. /*
  491. * Use ntfs_ffs() instead of ffs() to enable the compiler to
  492. * optimize log_page_size and log_page_bits into constants.
  493. */
  494. log_page_bits = ntfs_ffs(log_page_size) - 1;
  495. size &= ~(s64)(log_page_size - 1);
  496. /*
  497. * Ensure the log file is big enough to store at least the two restart
  498. * pages and the minimum number of log record pages.
  499. */
  500. if (size < log_page_size * 2 || (size - log_page_size * 2) >>
  501. log_page_bits < MinLogRecordPages) {
  502. ntfs_error(vol->sb, "$LogFile is too small.");
  503. return false;
  504. }
  505. /*
  506. * Read through the file looking for a restart page. Since the restart
  507. * page header is at the beginning of a page we only need to search at
  508. * what could be the beginning of a page (for each page size) rather
  509. * than scanning the whole file byte by byte. If all potential places
  510. * contain empty and uninitialzed records, the log file can be assumed
  511. * to be empty.
  512. */
  513. for (pos = 0; pos < size; pos <<= 1) {
  514. pgoff_t idx = pos >> PAGE_SHIFT;
  515. if (!page || page->index != idx) {
  516. if (page)
  517. ntfs_unmap_page(page);
  518. page = ntfs_map_page(mapping, idx);
  519. if (IS_ERR(page)) {
  520. ntfs_error(vol->sb, "Error mapping $LogFile "
  521. "page (index %lu).", idx);
  522. goto err_out;
  523. }
  524. }
  525. kaddr = (u8*)page_address(page) + (pos & ~PAGE_MASK);
  526. /*
  527. * A non-empty block means the logfile is not empty while an
  528. * empty block after a non-empty block has been encountered
  529. * means we are done.
  530. */
  531. if (!ntfs_is_empty_recordp((le32*)kaddr))
  532. logfile_is_empty = false;
  533. else if (!logfile_is_empty)
  534. break;
  535. /*
  536. * A log record page means there cannot be a restart page after
  537. * this so no need to continue searching.
  538. */
  539. if (ntfs_is_rcrd_recordp((le32*)kaddr))
  540. break;
  541. /* If not a (modified by chkdsk) restart page, continue. */
  542. if (!ntfs_is_rstr_recordp((le32*)kaddr) &&
  543. !ntfs_is_chkd_recordp((le32*)kaddr)) {
  544. if (!pos)
  545. pos = NTFS_BLOCK_SIZE >> 1;
  546. continue;
  547. }
  548. /*
  549. * Check the (modified by chkdsk) restart page for consistency
  550. * and get a copy of the complete multi sector transfer
  551. * deprotected restart page.
  552. */
  553. err = ntfs_check_and_load_restart_page(log_vi,
  554. (RESTART_PAGE_HEADER*)kaddr, pos,
  555. !rstr1_ph ? &rstr1_ph : &rstr2_ph,
  556. !rstr1_ph ? &rstr1_lsn : &rstr2_lsn);
  557. if (!err) {
  558. /*
  559. * If we have now found the first (modified by chkdsk)
  560. * restart page, continue looking for the second one.
  561. */
  562. if (!pos) {
  563. pos = NTFS_BLOCK_SIZE >> 1;
  564. continue;
  565. }
  566. /*
  567. * We have now found the second (modified by chkdsk)
  568. * restart page, so we can stop looking.
  569. */
  570. break;
  571. }
  572. /*
  573. * Error output already done inside the function. Note, we do
  574. * not abort if the restart page was invalid as we might still
  575. * find a valid one further in the file.
  576. */
  577. if (err != -EINVAL) {
  578. ntfs_unmap_page(page);
  579. goto err_out;
  580. }
  581. /* Continue looking. */
  582. if (!pos)
  583. pos = NTFS_BLOCK_SIZE >> 1;
  584. }
  585. if (page)
  586. ntfs_unmap_page(page);
  587. if (logfile_is_empty) {
  588. NVolSetLogFileEmpty(vol);
  589. is_empty:
  590. ntfs_debug("Done. ($LogFile is empty.)");
  591. return true;
  592. }
  593. if (!rstr1_ph) {
  594. BUG_ON(rstr2_ph);
  595. ntfs_error(vol->sb, "Did not find any restart pages in "
  596. "$LogFile and it was not empty.");
  597. return false;
  598. }
  599. /* If both restart pages were found, use the more recent one. */
  600. if (rstr2_ph) {
  601. /*
  602. * If the second restart area is more recent, switch to it.
  603. * Otherwise just throw it away.
  604. */
  605. if (rstr2_lsn > rstr1_lsn) {
  606. ntfs_debug("Using second restart page as it is more "
  607. "recent.");
  608. ntfs_free(rstr1_ph);
  609. rstr1_ph = rstr2_ph;
  610. /* rstr1_lsn = rstr2_lsn; */
  611. } else {
  612. ntfs_debug("Using first restart page as it is more "
  613. "recent.");
  614. ntfs_free(rstr2_ph);
  615. }
  616. rstr2_ph = NULL;
  617. }
  618. /* All consistency checks passed. */
  619. if (rp)
  620. *rp = rstr1_ph;
  621. else
  622. ntfs_free(rstr1_ph);
  623. ntfs_debug("Done.");
  624. return true;
  625. err_out:
  626. if (rstr1_ph)
  627. ntfs_free(rstr1_ph);
  628. return false;
  629. }
  630. /**
  631. * ntfs_is_logfile_clean - check in the journal if the volume is clean
  632. * @log_vi: struct inode of loaded journal $LogFile to check
  633. * @rp: copy of the current restart page
  634. *
  635. * Analyze the $LogFile journal and return 'true' if it indicates the volume was
  636. * shutdown cleanly and 'false' if not.
  637. *
  638. * At present we only look at the two restart pages and ignore the log record
  639. * pages. This is a little bit crude in that there will be a very small number
  640. * of cases where we think that a volume is dirty when in fact it is clean.
  641. * This should only affect volumes that have not been shutdown cleanly but did
  642. * not have any pending, non-check-pointed i/o, i.e. they were completely idle
  643. * at least for the five seconds preceding the unclean shutdown.
  644. *
  645. * This function assumes that the $LogFile journal has already been consistency
  646. * checked by a call to ntfs_check_logfile() and in particular if the $LogFile
  647. * is empty this function requires that NVolLogFileEmpty() is true otherwise an
  648. * empty volume will be reported as dirty.
  649. */
  650. bool ntfs_is_logfile_clean(struct inode *log_vi, const RESTART_PAGE_HEADER *rp)
  651. {
  652. ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
  653. RESTART_AREA *ra;
  654. ntfs_debug("Entering.");
  655. /* An empty $LogFile must have been clean before it got emptied. */
  656. if (NVolLogFileEmpty(vol)) {
  657. ntfs_debug("Done. ($LogFile is empty.)");
  658. return true;
  659. }
  660. BUG_ON(!rp);
  661. if (!ntfs_is_rstr_record(rp->magic) &&
  662. !ntfs_is_chkd_record(rp->magic)) {
  663. ntfs_error(vol->sb, "Restart page buffer is invalid. This is "
  664. "probably a bug in that the $LogFile should "
  665. "have been consistency checked before calling "
  666. "this function.");
  667. return false;
  668. }
  669. ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset));
  670. /*
  671. * If the $LogFile has active clients, i.e. it is open, and we do not
  672. * have the RESTART_VOLUME_IS_CLEAN bit set in the restart area flags,
  673. * we assume there was an unclean shutdown.
  674. */
  675. if (ra->client_in_use_list != LOGFILE_NO_CLIENT &&
  676. !(ra->flags & RESTART_VOLUME_IS_CLEAN)) {
  677. ntfs_debug("Done. $LogFile indicates a dirty shutdown.");
  678. return false;
  679. }
  680. /* $LogFile indicates a clean shutdown. */
  681. ntfs_debug("Done. $LogFile indicates a clean shutdown.");
  682. return true;
  683. }
  684. /**
  685. * ntfs_empty_logfile - empty the contents of the $LogFile journal
  686. * @log_vi: struct inode of loaded journal $LogFile to empty
  687. *
  688. * Empty the contents of the $LogFile journal @log_vi and return 'true' on
  689. * success and 'false' on error.
  690. *
  691. * This function assumes that the $LogFile journal has already been consistency
  692. * checked by a call to ntfs_check_logfile() and that ntfs_is_logfile_clean()
  693. * has been used to ensure that the $LogFile is clean.
  694. */
  695. bool ntfs_empty_logfile(struct inode *log_vi)
  696. {
  697. VCN vcn, end_vcn;
  698. ntfs_inode *log_ni = NTFS_I(log_vi);
  699. ntfs_volume *vol = log_ni->vol;
  700. struct super_block *sb = vol->sb;
  701. runlist_element *rl;
  702. unsigned long flags;
  703. unsigned block_size, block_size_bits;
  704. int err;
  705. bool should_wait = true;
  706. ntfs_debug("Entering.");
  707. if (NVolLogFileEmpty(vol)) {
  708. ntfs_debug("Done.");
  709. return true;
  710. }
  711. /*
  712. * We cannot use ntfs_attr_set() because we may be still in the middle
  713. * of a mount operation. Thus we do the emptying by hand by first
  714. * zapping the page cache pages for the $LogFile/$DATA attribute and
  715. * then emptying each of the buffers in each of the clusters specified
  716. * by the runlist by hand.
  717. */
  718. block_size = sb->s_blocksize;
  719. block_size_bits = sb->s_blocksize_bits;
  720. vcn = 0;
  721. read_lock_irqsave(&log_ni->size_lock, flags);
  722. end_vcn = (log_ni->initialized_size + vol->cluster_size_mask) >>
  723. vol->cluster_size_bits;
  724. read_unlock_irqrestore(&log_ni->size_lock, flags);
  725. truncate_inode_pages(log_vi->i_mapping, 0);
  726. down_write(&log_ni->runlist.lock);
  727. rl = log_ni->runlist.rl;
  728. if (unlikely(!rl || vcn < rl->vcn || !rl->length)) {
  729. map_vcn:
  730. err = ntfs_map_runlist_nolock(log_ni, vcn, NULL);
  731. if (err) {
  732. ntfs_error(sb, "Failed to map runlist fragment (error "
  733. "%d).", -err);
  734. goto err;
  735. }
  736. rl = log_ni->runlist.rl;
  737. BUG_ON(!rl || vcn < rl->vcn || !rl->length);
  738. }
  739. /* Seek to the runlist element containing @vcn. */
  740. while (rl->length && vcn >= rl[1].vcn)
  741. rl++;
  742. do {
  743. LCN lcn;
  744. sector_t block, end_block;
  745. s64 len;
  746. /*
  747. * If this run is not mapped map it now and start again as the
  748. * runlist will have been updated.
  749. */
  750. lcn = rl->lcn;
  751. if (unlikely(lcn == LCN_RL_NOT_MAPPED)) {
  752. vcn = rl->vcn;
  753. goto map_vcn;
  754. }
  755. /* If this run is not valid abort with an error. */
  756. if (unlikely(!rl->length || lcn < LCN_HOLE))
  757. goto rl_err;
  758. /* Skip holes. */
  759. if (lcn == LCN_HOLE)
  760. continue;
  761. block = lcn << vol->cluster_size_bits >> block_size_bits;
  762. len = rl->length;
  763. if (rl[1].vcn > end_vcn)
  764. len = end_vcn - rl->vcn;
  765. end_block = (lcn + len) << vol->cluster_size_bits >>
  766. block_size_bits;
  767. /* Iterate over the blocks in the run and empty them. */
  768. do {
  769. struct buffer_head *bh;
  770. /* Obtain the buffer, possibly not uptodate. */
  771. bh = sb_getblk(sb, block);
  772. BUG_ON(!bh);
  773. /* Setup buffer i/o submission. */
  774. lock_buffer(bh);
  775. bh->b_end_io = end_buffer_write_sync;
  776. get_bh(bh);
  777. /* Set the entire contents of the buffer to 0xff. */
  778. memset(bh->b_data, -1, block_size);
  779. if (!buffer_uptodate(bh))
  780. set_buffer_uptodate(bh);
  781. if (buffer_dirty(bh))
  782. clear_buffer_dirty(bh);
  783. /*
  784. * Submit the buffer and wait for i/o to complete but
  785. * only for the first buffer so we do not miss really
  786. * serious i/o errors. Once the first buffer has
  787. * completed ignore errors afterwards as we can assume
  788. * that if one buffer worked all of them will work.
  789. */
  790. submit_bh(REQ_OP_WRITE, bh);
  791. if (should_wait) {
  792. should_wait = false;
  793. wait_on_buffer(bh);
  794. if (unlikely(!buffer_uptodate(bh)))
  795. goto io_err;
  796. }
  797. brelse(bh);
  798. } while (++block < end_block);
  799. } while ((++rl)->vcn < end_vcn);
  800. up_write(&log_ni->runlist.lock);
  801. /*
  802. * Zap the pages again just in case any got instantiated whilst we were
  803. * emptying the blocks by hand. FIXME: We may not have completed
  804. * writing to all the buffer heads yet so this may happen too early.
  805. * We really should use a kernel thread to do the emptying
  806. * asynchronously and then we can also set the volume dirty and output
  807. * an error message if emptying should fail.
  808. */
  809. truncate_inode_pages(log_vi->i_mapping, 0);
  810. /* Set the flag so we do not have to do it again on remount. */
  811. NVolSetLogFileEmpty(vol);
  812. ntfs_debug("Done.");
  813. return true;
  814. io_err:
  815. ntfs_error(sb, "Failed to write buffer. Unmount and run chkdsk.");
  816. goto dirty_err;
  817. rl_err:
  818. ntfs_error(sb, "Runlist is corrupt. Unmount and run chkdsk.");
  819. dirty_err:
  820. NVolSetErrors(vol);
  821. err = -EIO;
  822. err:
  823. up_write(&log_ni->runlist.lock);
  824. ntfs_error(sb, "Failed to fill $LogFile with 0xff bytes (error %d).",
  825. -err);
  826. return false;
  827. }
  828. #endif /* NTFS_RW */