recovery.c 22 KB

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