recovery.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * linux/fs/jbd2/recovery.c
  4. *
  5. * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
  6. *
  7. * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
  8. *
  9. * Journal recovery routines for the generic filesystem journaling code;
  10. * part of the ext2fs journaling system.
  11. */
  12. #ifndef __KERNEL__
  13. #include "jfs_user.h"
  14. #else
  15. #include <linux/time.h>
  16. #include <linux/fs.h>
  17. #include <linux/jbd2.h>
  18. #include <linux/errno.h>
  19. #include <linux/crc32.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/string_choices.h>
  22. #endif
  23. /*
  24. * Maintain information about the progress of the recovery job, so that
  25. * the different passes can carry information between them.
  26. */
  27. struct recovery_info
  28. {
  29. tid_t start_transaction;
  30. tid_t end_transaction;
  31. unsigned long head_block;
  32. int nr_replays;
  33. int nr_revokes;
  34. int nr_revoke_hits;
  35. };
  36. static int do_one_pass(journal_t *journal,
  37. struct recovery_info *info, enum passtype pass);
  38. static int scan_revoke_records(journal_t *, enum passtype, struct buffer_head *,
  39. tid_t, struct recovery_info *);
  40. #ifdef __KERNEL__
  41. /* Release readahead buffers after use */
  42. static void journal_brelse_array(struct buffer_head *b[], int n)
  43. {
  44. while (--n >= 0)
  45. brelse (b[n]);
  46. }
  47. /*
  48. * When reading from the journal, we are going through the block device
  49. * layer directly and so there is no readahead being done for us. We
  50. * need to implement any readahead ourselves if we want it to happen at
  51. * all. Recovery is basically one long sequential read, so make sure we
  52. * do the IO in reasonably large chunks.
  53. *
  54. * This is not so critical that we need to be enormously clever about
  55. * the readahead size, though. 128K is a purely arbitrary, good-enough
  56. * fixed value.
  57. */
  58. #define MAXBUF 8
  59. static void do_readahead(journal_t *journal, unsigned int start)
  60. {
  61. unsigned int max, nbufs, next;
  62. unsigned long long blocknr;
  63. struct buffer_head *bh;
  64. struct buffer_head * bufs[MAXBUF];
  65. /* Do up to 128K of readahead */
  66. max = start + (128 * 1024 / journal->j_blocksize);
  67. if (max > journal->j_total_len)
  68. max = journal->j_total_len;
  69. /* Do the readahead itself. We'll submit MAXBUF buffer_heads at
  70. * a time to the block device IO layer. */
  71. nbufs = 0;
  72. for (next = start; next < max; next++) {
  73. int err = jbd2_journal_bmap(journal, next, &blocknr);
  74. if (err) {
  75. printk(KERN_ERR "JBD2: bad block at offset %u\n",
  76. next);
  77. goto failed;
  78. }
  79. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  80. if (!bh)
  81. goto failed;
  82. if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
  83. bufs[nbufs++] = bh;
  84. if (nbufs == MAXBUF) {
  85. bh_readahead_batch(nbufs, bufs, 0);
  86. journal_brelse_array(bufs, nbufs);
  87. nbufs = 0;
  88. }
  89. } else
  90. brelse(bh);
  91. }
  92. if (nbufs)
  93. bh_readahead_batch(nbufs, bufs, 0);
  94. failed:
  95. if (nbufs)
  96. journal_brelse_array(bufs, nbufs);
  97. }
  98. #endif /* __KERNEL__ */
  99. /*
  100. * Read a block from the journal
  101. */
  102. static int jread(struct buffer_head **bhp, journal_t *journal,
  103. unsigned int offset)
  104. {
  105. int err;
  106. unsigned long long blocknr;
  107. struct buffer_head *bh;
  108. *bhp = NULL;
  109. if (offset >= journal->j_total_len) {
  110. printk(KERN_ERR "JBD2: corrupted journal superblock\n");
  111. return -EFSCORRUPTED;
  112. }
  113. err = jbd2_journal_bmap(journal, offset, &blocknr);
  114. if (err) {
  115. printk(KERN_ERR "JBD2: bad block at offset %u\n",
  116. offset);
  117. return err;
  118. }
  119. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  120. if (!bh)
  121. return -ENOMEM;
  122. if (!buffer_uptodate(bh)) {
  123. /*
  124. * If this is a brand new buffer, start readahead.
  125. * Otherwise, we assume we are already reading it.
  126. */
  127. bool need_readahead = !buffer_req(bh);
  128. bh_read_nowait(bh, 0);
  129. if (need_readahead)
  130. do_readahead(journal, offset);
  131. wait_on_buffer(bh);
  132. }
  133. if (!buffer_uptodate(bh)) {
  134. printk(KERN_ERR "JBD2: Failed to read block at offset %u\n",
  135. offset);
  136. brelse(bh);
  137. return -EIO;
  138. }
  139. *bhp = bh;
  140. return 0;
  141. }
  142. static int jbd2_descriptor_block_csum_verify(journal_t *j, void *buf)
  143. {
  144. struct jbd2_journal_block_tail *tail;
  145. __be32 provided;
  146. __u32 calculated;
  147. if (!jbd2_journal_has_csum_v2or3(j))
  148. return 1;
  149. tail = (struct jbd2_journal_block_tail *)((char *)buf +
  150. j->j_blocksize - sizeof(struct jbd2_journal_block_tail));
  151. provided = tail->t_checksum;
  152. tail->t_checksum = 0;
  153. calculated = jbd2_chksum(j->j_csum_seed, buf, j->j_blocksize);
  154. tail->t_checksum = provided;
  155. return provided == cpu_to_be32(calculated);
  156. }
  157. /*
  158. * Count the number of in-use tags in a journal descriptor block.
  159. */
  160. static int count_tags(journal_t *journal, struct buffer_head *bh)
  161. {
  162. char * tagp;
  163. journal_block_tag_t tag;
  164. int nr = 0, size = journal->j_blocksize;
  165. int tag_bytes = journal_tag_bytes(journal);
  166. if (jbd2_journal_has_csum_v2or3(journal))
  167. size -= sizeof(struct jbd2_journal_block_tail);
  168. tagp = &bh->b_data[sizeof(journal_header_t)];
  169. while ((tagp - bh->b_data + tag_bytes) <= size) {
  170. memcpy(&tag, tagp, sizeof(tag));
  171. nr++;
  172. tagp += tag_bytes;
  173. if (!(tag.t_flags & cpu_to_be16(JBD2_FLAG_SAME_UUID)))
  174. tagp += 16;
  175. if (tag.t_flags & cpu_to_be16(JBD2_FLAG_LAST_TAG))
  176. break;
  177. }
  178. return nr;
  179. }
  180. /* Make sure we wrap around the log correctly! */
  181. #define wrap(journal, var) \
  182. do { \
  183. if (var >= (journal)->j_last) \
  184. var -= ((journal)->j_last - (journal)->j_first); \
  185. } while (0)
  186. static int fc_do_one_pass(journal_t *journal,
  187. struct recovery_info *info, enum passtype pass)
  188. {
  189. unsigned int expected_commit_id = info->end_transaction;
  190. unsigned long next_fc_block;
  191. struct buffer_head *bh;
  192. int err = 0;
  193. next_fc_block = journal->j_fc_first;
  194. if (!journal->j_fc_replay_callback)
  195. return 0;
  196. while (next_fc_block <= journal->j_fc_last) {
  197. jbd2_debug(3, "Fast commit replay: next block %ld\n",
  198. next_fc_block);
  199. err = jread(&bh, journal, next_fc_block);
  200. if (err) {
  201. jbd2_debug(3, "Fast commit replay: read error\n");
  202. break;
  203. }
  204. err = journal->j_fc_replay_callback(journal, bh, pass,
  205. next_fc_block - journal->j_fc_first,
  206. expected_commit_id);
  207. brelse(bh);
  208. next_fc_block++;
  209. if (err < 0 || err == JBD2_FC_REPLAY_STOP)
  210. break;
  211. err = 0;
  212. }
  213. if (err)
  214. jbd2_debug(3, "Fast commit replay failed, err = %d\n", err);
  215. return err;
  216. }
  217. /**
  218. * jbd2_journal_recover - recovers a on-disk journal
  219. * @journal: the journal to recover
  220. *
  221. * The primary function for recovering the log contents when mounting a
  222. * journaled device.
  223. *
  224. * Recovery is done in three passes. In the first pass, we look for the
  225. * end of the log. In the second, we assemble the list of revoke
  226. * blocks. In the third and final pass, we replay any un-revoked blocks
  227. * in the log.
  228. */
  229. int jbd2_journal_recover(journal_t *journal)
  230. {
  231. int err, err2;
  232. struct recovery_info info;
  233. memset(&info, 0, sizeof(info));
  234. /*
  235. * The journal superblock's s_start field (the current log head)
  236. * is always zero if, and only if, the journal was cleanly
  237. * unmounted. We use its in-memory version j_tail here because
  238. * jbd2_journal_wipe() could have updated it without updating journal
  239. * superblock.
  240. */
  241. if (!journal->j_tail) {
  242. journal_superblock_t *sb = journal->j_superblock;
  243. jbd2_debug(1, "No recovery required, last transaction %d, head block %u\n",
  244. be32_to_cpu(sb->s_sequence), be32_to_cpu(sb->s_head));
  245. journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
  246. journal->j_head = be32_to_cpu(sb->s_head);
  247. return 0;
  248. }
  249. err = do_one_pass(journal, &info, PASS_SCAN);
  250. if (!err)
  251. err = do_one_pass(journal, &info, PASS_REVOKE);
  252. if (!err)
  253. err = do_one_pass(journal, &info, PASS_REPLAY);
  254. jbd2_debug(1, "JBD2: recovery, exit status %d, "
  255. "recovered transactions %u to %u\n",
  256. err, info.start_transaction, info.end_transaction);
  257. jbd2_debug(1, "JBD2: Replayed %d and revoked %d/%d blocks\n",
  258. info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
  259. /* Restart the log at the next transaction ID, thus invalidating
  260. * any existing commit records in the log. */
  261. journal->j_transaction_sequence = ++info.end_transaction;
  262. journal->j_head = info.head_block;
  263. jbd2_debug(1, "JBD2: last transaction %d, head block %lu\n",
  264. journal->j_transaction_sequence, journal->j_head);
  265. jbd2_journal_clear_revoke(journal);
  266. /* Free revoke table allocated for replay */
  267. if (journal->j_revoke != journal->j_revoke_table[0] &&
  268. journal->j_revoke != journal->j_revoke_table[1]) {
  269. jbd2_journal_destroy_revoke_table(journal->j_revoke);
  270. journal->j_revoke = journal->j_revoke_table[1];
  271. }
  272. err2 = sync_blockdev(journal->j_fs_dev);
  273. if (!err)
  274. err = err2;
  275. err2 = jbd2_check_fs_dev_write_error(journal);
  276. if (!err)
  277. err = err2;
  278. /* Make sure all replayed data is on permanent storage */
  279. if (journal->j_flags & JBD2_BARRIER) {
  280. err2 = blkdev_issue_flush(journal->j_fs_dev);
  281. if (!err)
  282. err = err2;
  283. }
  284. return err;
  285. }
  286. /**
  287. * jbd2_journal_skip_recovery - Start journal and wipe exiting records
  288. * @journal: journal to startup
  289. *
  290. * Locate any valid recovery information from the journal and set up the
  291. * journal structures in memory to ignore it (presumably because the
  292. * caller has evidence that it is out of date).
  293. * This function doesn't appear to be exported..
  294. *
  295. * We perform one pass over the journal to allow us to tell the user how
  296. * much recovery information is being erased, and to let us initialise
  297. * the journal transaction sequence numbers to the next unused ID.
  298. */
  299. int jbd2_journal_skip_recovery(journal_t *journal)
  300. {
  301. int err;
  302. struct recovery_info info;
  303. memset (&info, 0, sizeof(info));
  304. err = do_one_pass(journal, &info, PASS_SCAN);
  305. if (err) {
  306. printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
  307. ++journal->j_transaction_sequence;
  308. journal->j_head = journal->j_first;
  309. } else {
  310. #ifdef CONFIG_JBD2_DEBUG
  311. int dropped = info.end_transaction -
  312. be32_to_cpu(journal->j_superblock->s_sequence);
  313. jbd2_debug(1,
  314. "JBD2: ignoring %d transaction%s from the journal.\n",
  315. dropped, str_plural(dropped));
  316. #endif
  317. journal->j_transaction_sequence = ++info.end_transaction;
  318. journal->j_head = info.head_block;
  319. }
  320. journal->j_tail = 0;
  321. return err;
  322. }
  323. static inline unsigned long long read_tag_block(journal_t *journal,
  324. journal_block_tag_t *tag)
  325. {
  326. unsigned long long block = be32_to_cpu(tag->t_blocknr);
  327. if (jbd2_has_feature_64bit(journal))
  328. block |= (u64)be32_to_cpu(tag->t_blocknr_high) << 32;
  329. return block;
  330. }
  331. /*
  332. * calc_chksums calculates the checksums for the blocks described in the
  333. * descriptor block.
  334. */
  335. static int calc_chksums(journal_t *journal, struct buffer_head *bh,
  336. unsigned long *next_log_block, __u32 *crc32_sum)
  337. {
  338. int i, num_blks, err;
  339. unsigned long io_block;
  340. struct buffer_head *obh;
  341. num_blks = count_tags(journal, bh);
  342. /* Calculate checksum of the descriptor block. */
  343. *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
  344. for (i = 0; i < num_blks; i++) {
  345. io_block = (*next_log_block)++;
  346. wrap(journal, *next_log_block);
  347. err = jread(&obh, journal, io_block);
  348. if (err) {
  349. printk(KERN_ERR "JBD2: IO error %d recovering block "
  350. "%lu in log\n", err, io_block);
  351. return 1;
  352. } else {
  353. *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
  354. obh->b_size);
  355. }
  356. put_bh(obh);
  357. }
  358. return 0;
  359. }
  360. static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
  361. {
  362. struct commit_header *h;
  363. __be32 provided;
  364. __u32 calculated;
  365. if (!jbd2_journal_has_csum_v2or3(j))
  366. return 1;
  367. h = buf;
  368. provided = h->h_chksum[0];
  369. h->h_chksum[0] = 0;
  370. calculated = jbd2_chksum(j->j_csum_seed, buf, j->j_blocksize);
  371. h->h_chksum[0] = provided;
  372. return provided == cpu_to_be32(calculated);
  373. }
  374. static bool jbd2_commit_block_csum_verify_partial(journal_t *j, void *buf)
  375. {
  376. struct commit_header *h;
  377. __be32 provided;
  378. __u32 calculated;
  379. void *tmpbuf;
  380. tmpbuf = kzalloc(j->j_blocksize, GFP_KERNEL);
  381. if (!tmpbuf)
  382. return false;
  383. memcpy(tmpbuf, buf, sizeof(struct commit_header));
  384. h = tmpbuf;
  385. provided = h->h_chksum[0];
  386. h->h_chksum[0] = 0;
  387. calculated = jbd2_chksum(j->j_csum_seed, tmpbuf, j->j_blocksize);
  388. kfree(tmpbuf);
  389. return provided == cpu_to_be32(calculated);
  390. }
  391. static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
  392. journal_block_tag3_t *tag3,
  393. void *buf, __u32 sequence)
  394. {
  395. __u32 csum32;
  396. __be32 seq;
  397. if (!jbd2_journal_has_csum_v2or3(j))
  398. return 1;
  399. seq = cpu_to_be32(sequence);
  400. csum32 = jbd2_chksum(j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
  401. csum32 = jbd2_chksum(csum32, buf, j->j_blocksize);
  402. if (jbd2_has_feature_csum3(j))
  403. return tag3->t_checksum == cpu_to_be32(csum32);
  404. else
  405. return tag->t_checksum == cpu_to_be16(csum32);
  406. }
  407. static __always_inline int jbd2_do_replay(journal_t *journal,
  408. struct recovery_info *info,
  409. struct buffer_head *bh,
  410. unsigned long *next_log_block,
  411. unsigned int next_commit_ID)
  412. {
  413. char *tagp;
  414. int flags;
  415. int ret = 0;
  416. int tag_bytes = journal_tag_bytes(journal);
  417. int descr_csum_size = 0;
  418. unsigned long io_block;
  419. journal_block_tag_t tag;
  420. struct buffer_head *obh;
  421. struct buffer_head *nbh;
  422. if (jbd2_journal_has_csum_v2or3(journal))
  423. descr_csum_size = sizeof(struct jbd2_journal_block_tail);
  424. tagp = &bh->b_data[sizeof(journal_header_t)];
  425. while (tagp - bh->b_data + tag_bytes <=
  426. journal->j_blocksize - descr_csum_size) {
  427. int err;
  428. memcpy(&tag, tagp, sizeof(tag));
  429. flags = be16_to_cpu(tag.t_flags);
  430. io_block = (*next_log_block)++;
  431. wrap(journal, *next_log_block);
  432. err = jread(&obh, journal, io_block);
  433. if (err) {
  434. /* Recover what we can, but report failure at the end. */
  435. ret = err;
  436. pr_err("JBD2: IO error %d recovering block %lu in log\n",
  437. err, io_block);
  438. } else {
  439. unsigned long long blocknr;
  440. J_ASSERT(obh != NULL);
  441. blocknr = read_tag_block(journal, &tag);
  442. /* If the block has been revoked, then we're all done here. */
  443. if (jbd2_journal_test_revoke(journal, blocknr,
  444. next_commit_ID)) {
  445. brelse(obh);
  446. ++info->nr_revoke_hits;
  447. goto skip_write;
  448. }
  449. /* Look for block corruption */
  450. if (!jbd2_block_tag_csum_verify(journal, &tag,
  451. (journal_block_tag3_t *)tagp,
  452. obh->b_data, next_commit_ID)) {
  453. brelse(obh);
  454. ret = -EFSBADCRC;
  455. pr_err("JBD2: Invalid checksum recovering data block %llu in journal block %lu\n",
  456. blocknr, io_block);
  457. goto skip_write;
  458. }
  459. /* Find a buffer for the new data being restored */
  460. nbh = __getblk(journal->j_fs_dev, blocknr,
  461. journal->j_blocksize);
  462. if (nbh == NULL) {
  463. pr_err("JBD2: Out of memory during recovery.\n");
  464. brelse(obh);
  465. return -ENOMEM;
  466. }
  467. lock_buffer(nbh);
  468. memcpy(nbh->b_data, obh->b_data, journal->j_blocksize);
  469. if (flags & JBD2_FLAG_ESCAPE) {
  470. *((__be32 *)nbh->b_data) =
  471. cpu_to_be32(JBD2_MAGIC_NUMBER);
  472. }
  473. BUFFER_TRACE(nbh, "marking dirty");
  474. set_buffer_uptodate(nbh);
  475. mark_buffer_dirty(nbh);
  476. BUFFER_TRACE(nbh, "marking uptodate");
  477. ++info->nr_replays;
  478. unlock_buffer(nbh);
  479. brelse(obh);
  480. brelse(nbh);
  481. }
  482. skip_write:
  483. tagp += tag_bytes;
  484. if (!(flags & JBD2_FLAG_SAME_UUID))
  485. tagp += 16;
  486. if (flags & JBD2_FLAG_LAST_TAG)
  487. break;
  488. }
  489. return ret;
  490. }
  491. static int do_one_pass(journal_t *journal,
  492. struct recovery_info *info, enum passtype pass)
  493. {
  494. unsigned int first_commit_ID, next_commit_ID;
  495. unsigned long next_log_block, head_block;
  496. int err, success = 0;
  497. journal_superblock_t * sb;
  498. journal_header_t * tmp;
  499. struct buffer_head *bh = NULL;
  500. unsigned int sequence;
  501. int blocktype;
  502. __u32 crc32_sum = ~0; /* Transactional Checksums */
  503. bool need_check_commit_time = false;
  504. __u64 last_trans_commit_time = 0, commit_time;
  505. /*
  506. * First thing is to establish what we expect to find in the log
  507. * (in terms of transaction IDs), and where (in terms of log
  508. * block offsets): query the superblock.
  509. */
  510. sb = journal->j_superblock;
  511. next_commit_ID = be32_to_cpu(sb->s_sequence);
  512. next_log_block = be32_to_cpu(sb->s_start);
  513. head_block = next_log_block;
  514. first_commit_ID = next_commit_ID;
  515. if (pass == PASS_SCAN)
  516. info->start_transaction = first_commit_ID;
  517. else if (pass == PASS_REVOKE) {
  518. /*
  519. * Would the default revoke table have too long hash chains
  520. * during replay?
  521. */
  522. if (info->nr_revokes > JOURNAL_REVOKE_DEFAULT_HASH * 16) {
  523. unsigned int hash_size;
  524. /*
  525. * Aim for average chain length of 8, limit at 1M
  526. * entries to avoid problems with malicious
  527. * filesystems.
  528. */
  529. hash_size = min(roundup_pow_of_two(info->nr_revokes / 8),
  530. 1U << 20);
  531. journal->j_revoke =
  532. jbd2_journal_init_revoke_table(hash_size);
  533. if (!journal->j_revoke) {
  534. printk(KERN_ERR
  535. "JBD2: failed to allocate revoke table for replay with %u entries. "
  536. "Journal replay may be slow.\n", hash_size);
  537. journal->j_revoke = journal->j_revoke_table[1];
  538. }
  539. }
  540. }
  541. jbd2_debug(1, "Starting recovery pass %d\n", pass);
  542. /*
  543. * Now we walk through the log, transaction by transaction,
  544. * making sure that each transaction has a commit block in the
  545. * expected place. Each complete transaction gets replayed back
  546. * into the main filesystem.
  547. */
  548. while (1) {
  549. cond_resched();
  550. /* If we already know where to stop the log traversal,
  551. * check right now that we haven't gone past the end of
  552. * the log. */
  553. if (pass != PASS_SCAN)
  554. if (tid_geq(next_commit_ID, info->end_transaction))
  555. break;
  556. jbd2_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
  557. next_commit_ID, next_log_block, journal->j_last);
  558. /* Skip over each chunk of the transaction looking
  559. * either the next descriptor block or the final commit
  560. * record. */
  561. jbd2_debug(3, "JBD2: checking block %ld\n", next_log_block);
  562. brelse(bh);
  563. bh = NULL;
  564. err = jread(&bh, journal, next_log_block);
  565. if (err)
  566. goto failed;
  567. next_log_block++;
  568. wrap(journal, next_log_block);
  569. /* What kind of buffer is it?
  570. *
  571. * If it is a descriptor block, check that it has the
  572. * expected sequence number. Otherwise, we're all done
  573. * here. */
  574. tmp = (journal_header_t *)bh->b_data;
  575. if (tmp->h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER))
  576. break;
  577. blocktype = be32_to_cpu(tmp->h_blocktype);
  578. sequence = be32_to_cpu(tmp->h_sequence);
  579. jbd2_debug(3, "Found magic %d, sequence %d\n",
  580. blocktype, sequence);
  581. if (sequence != next_commit_ID)
  582. break;
  583. /* OK, we have a valid descriptor block which matches
  584. * all of the sequence number checks. What are we going
  585. * to do with it? That depends on the pass... */
  586. switch(blocktype) {
  587. case JBD2_DESCRIPTOR_BLOCK:
  588. /* Verify checksum first */
  589. if (!jbd2_descriptor_block_csum_verify(journal,
  590. bh->b_data)) {
  591. /*
  592. * PASS_SCAN can see stale blocks due to lazy
  593. * journal init. Don't error out on those yet.
  594. */
  595. if (pass != PASS_SCAN) {
  596. pr_err("JBD2: Invalid checksum recovering block %lu in log\n",
  597. next_log_block);
  598. err = -EFSBADCRC;
  599. goto failed;
  600. }
  601. need_check_commit_time = true;
  602. jbd2_debug(1,
  603. "invalid descriptor block found in %lu\n",
  604. next_log_block);
  605. }
  606. /* If it is a valid descriptor block, replay it
  607. * in pass REPLAY; if journal_checksums enabled, then
  608. * calculate checksums in PASS_SCAN, otherwise,
  609. * just skip over the blocks it describes. */
  610. if (pass != PASS_REPLAY) {
  611. if (pass == PASS_SCAN &&
  612. jbd2_has_feature_checksum(journal) &&
  613. !info->end_transaction) {
  614. if (calc_chksums(journal, bh,
  615. &next_log_block,
  616. &crc32_sum))
  617. break;
  618. continue;
  619. }
  620. next_log_block += count_tags(journal, bh);
  621. wrap(journal, next_log_block);
  622. continue;
  623. }
  624. /*
  625. * A descriptor block: we can now write all of the
  626. * data blocks. Yay, useful work is finally getting
  627. * done here!
  628. */
  629. err = jbd2_do_replay(journal, info, bh, &next_log_block,
  630. next_commit_ID);
  631. if (err) {
  632. if (err == -ENOMEM)
  633. goto failed;
  634. success = err;
  635. }
  636. continue;
  637. case JBD2_COMMIT_BLOCK:
  638. if (pass != PASS_SCAN) {
  639. next_commit_ID++;
  640. continue;
  641. }
  642. /* How to differentiate between interrupted commit
  643. * and journal corruption ?
  644. *
  645. * {nth transaction}
  646. * Checksum Verification Failed
  647. * |
  648. * ____________________
  649. * | |
  650. * async_commit sync_commit
  651. * | |
  652. * | GO TO NEXT "Journal Corruption"
  653. * | TRANSACTION
  654. * |
  655. * {(n+1)th transanction}
  656. * |
  657. * _______|______________
  658. * | |
  659. * Commit block found Commit block not found
  660. * | |
  661. * "Journal Corruption" |
  662. * _____________|_________
  663. * | |
  664. * nth trans corrupt OR nth trans
  665. * and (n+1)th interrupted interrupted
  666. * before commit block
  667. * could reach the disk.
  668. * (Cannot find the difference in above
  669. * mentioned conditions. Hence assume
  670. * "Interrupted Commit".)
  671. */
  672. commit_time = be64_to_cpu(
  673. ((struct commit_header *)bh->b_data)->h_commit_sec);
  674. /*
  675. * If need_check_commit_time is set, it means we are in
  676. * PASS_SCAN and csum verify failed before. If
  677. * commit_time is increasing, it's the same journal,
  678. * otherwise it is stale journal block, just end this
  679. * recovery.
  680. */
  681. if (need_check_commit_time) {
  682. if (commit_time >= last_trans_commit_time) {
  683. pr_err("JBD2: Invalid checksum found in transaction %u\n",
  684. next_commit_ID);
  685. err = -EFSBADCRC;
  686. goto failed;
  687. }
  688. ignore_crc_mismatch:
  689. /*
  690. * It likely does not belong to same journal,
  691. * just end this recovery with success.
  692. */
  693. jbd2_debug(1, "JBD2: Invalid checksum ignored in transaction %u, likely stale data\n",
  694. next_commit_ID);
  695. goto done;
  696. }
  697. /*
  698. * Found an expected commit block: if checksums
  699. * are present, verify them in PASS_SCAN; else not
  700. * much to do other than move on to the next sequence
  701. * number.
  702. */
  703. if (jbd2_has_feature_checksum(journal)) {
  704. struct commit_header *cbh =
  705. (struct commit_header *)bh->b_data;
  706. unsigned found_chksum =
  707. be32_to_cpu(cbh->h_chksum[0]);
  708. if (info->end_transaction) {
  709. journal->j_failed_commit =
  710. info->end_transaction;
  711. break;
  712. }
  713. /* Neither checksum match nor unused? */
  714. if (!((crc32_sum == found_chksum &&
  715. cbh->h_chksum_type ==
  716. JBD2_CRC32_CHKSUM &&
  717. cbh->h_chksum_size ==
  718. JBD2_CRC32_CHKSUM_SIZE) ||
  719. (cbh->h_chksum_type == 0 &&
  720. cbh->h_chksum_size == 0 &&
  721. found_chksum == 0)))
  722. goto chksum_error;
  723. crc32_sum = ~0;
  724. goto chksum_ok;
  725. }
  726. if (jbd2_commit_block_csum_verify(journal, bh->b_data))
  727. goto chksum_ok;
  728. if (jbd2_commit_block_csum_verify_partial(journal,
  729. bh->b_data)) {
  730. pr_notice("JBD2: Find incomplete commit block in transaction %u block %lu\n",
  731. next_commit_ID, next_log_block);
  732. goto chksum_ok;
  733. }
  734. chksum_error:
  735. if (commit_time < last_trans_commit_time)
  736. goto ignore_crc_mismatch;
  737. info->end_transaction = next_commit_ID;
  738. info->head_block = head_block;
  739. if (!jbd2_has_feature_async_commit(journal)) {
  740. journal->j_failed_commit = next_commit_ID;
  741. break;
  742. }
  743. chksum_ok:
  744. last_trans_commit_time = commit_time;
  745. head_block = next_log_block;
  746. next_commit_ID++;
  747. continue;
  748. case JBD2_REVOKE_BLOCK:
  749. /*
  750. * If we aren't in the SCAN or REVOKE pass, then we can
  751. * just skip over this block.
  752. */
  753. if (pass != PASS_REVOKE && pass != PASS_SCAN)
  754. continue;
  755. /*
  756. * Check revoke block crc in pass_scan, if csum verify
  757. * failed, check commit block time later.
  758. */
  759. if (pass == PASS_SCAN &&
  760. !jbd2_descriptor_block_csum_verify(journal,
  761. bh->b_data)) {
  762. jbd2_debug(1, "JBD2: invalid revoke block found in %lu\n",
  763. next_log_block);
  764. need_check_commit_time = true;
  765. }
  766. err = scan_revoke_records(journal, pass, bh,
  767. next_commit_ID, info);
  768. if (err)
  769. goto failed;
  770. continue;
  771. default:
  772. jbd2_debug(3, "Unrecognised magic %d, end of scan.\n",
  773. blocktype);
  774. goto done;
  775. }
  776. }
  777. done:
  778. brelse(bh);
  779. /*
  780. * We broke out of the log scan loop: either we came to the
  781. * known end of the log or we found an unexpected block in the
  782. * log. If the latter happened, then we know that the "current"
  783. * transaction marks the end of the valid log.
  784. */
  785. if (pass == PASS_SCAN) {
  786. if (!info->end_transaction)
  787. info->end_transaction = next_commit_ID;
  788. if (!info->head_block)
  789. info->head_block = head_block;
  790. } else {
  791. /* It's really bad news if different passes end up at
  792. * different places (but possible due to IO errors). */
  793. if (info->end_transaction != next_commit_ID) {
  794. printk(KERN_ERR "JBD2: recovery pass %d ended at "
  795. "transaction %u, expected %u\n",
  796. pass, next_commit_ID, info->end_transaction);
  797. if (!success)
  798. success = -EIO;
  799. }
  800. }
  801. if (jbd2_has_feature_fast_commit(journal) && pass != PASS_REVOKE) {
  802. err = fc_do_one_pass(journal, info, pass);
  803. if (err)
  804. success = err;
  805. }
  806. return success;
  807. failed:
  808. brelse(bh);
  809. return err;
  810. }
  811. /* Scan a revoke record, marking all blocks mentioned as revoked. */
  812. static int scan_revoke_records(journal_t *journal, enum passtype pass,
  813. struct buffer_head *bh, tid_t sequence,
  814. struct recovery_info *info)
  815. {
  816. jbd2_journal_revoke_header_t *header;
  817. int offset, max;
  818. unsigned csum_size = 0;
  819. __u32 rcount;
  820. int record_len = 4;
  821. header = (jbd2_journal_revoke_header_t *) bh->b_data;
  822. offset = sizeof(jbd2_journal_revoke_header_t);
  823. rcount = be32_to_cpu(header->r_count);
  824. if (jbd2_journal_has_csum_v2or3(journal))
  825. csum_size = sizeof(struct jbd2_journal_block_tail);
  826. if (rcount > journal->j_blocksize - csum_size)
  827. return -EINVAL;
  828. max = rcount;
  829. if (jbd2_has_feature_64bit(journal))
  830. record_len = 8;
  831. if (pass == PASS_SCAN) {
  832. info->nr_revokes += (max - offset) / record_len;
  833. return 0;
  834. }
  835. while (offset + record_len <= max) {
  836. unsigned long long blocknr;
  837. int err;
  838. if (record_len == 4)
  839. blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
  840. else
  841. blocknr = be64_to_cpu(* ((__be64 *) (bh->b_data+offset)));
  842. offset += record_len;
  843. err = jbd2_journal_set_revoke(journal, blocknr, sequence);
  844. if (err)
  845. return err;
  846. }
  847. return 0;
  848. }