recovery.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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(READ, 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(READ, 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 -EIO;
  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_descr_block_csum_verify(journal_t *j,
  146. void *buf)
  147. {
  148. struct jbd2_journal_block_tail *tail;
  149. __be32 provided;
  150. __u32 calculated;
  151. if (!jbd2_journal_has_csum_v2or3(j))
  152. return 1;
  153. tail = (struct jbd2_journal_block_tail *)(buf + j->j_blocksize -
  154. sizeof(struct jbd2_journal_block_tail));
  155. provided = tail->t_checksum;
  156. tail->t_checksum = 0;
  157. calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
  158. tail->t_checksum = provided;
  159. return provided == cpu_to_be32(calculated);
  160. }
  161. /*
  162. * Count the number of in-use tags in a journal descriptor block.
  163. */
  164. static int count_tags(journal_t *journal, struct buffer_head *bh)
  165. {
  166. char * tagp;
  167. journal_block_tag_t * tag;
  168. int nr = 0, size = journal->j_blocksize;
  169. int tag_bytes = journal_tag_bytes(journal);
  170. if (jbd2_journal_has_csum_v2or3(journal))
  171. size -= sizeof(struct jbd2_journal_block_tail);
  172. tagp = &bh->b_data[sizeof(journal_header_t)];
  173. while ((tagp - bh->b_data + tag_bytes) <= size) {
  174. tag = (journal_block_tag_t *) tagp;
  175. nr++;
  176. tagp += tag_bytes;
  177. if (!(tag->t_flags & cpu_to_be16(JBD2_FLAG_SAME_UUID)))
  178. tagp += 16;
  179. if (tag->t_flags & cpu_to_be16(JBD2_FLAG_LAST_TAG))
  180. break;
  181. }
  182. return nr;
  183. }
  184. /* Make sure we wrap around the log correctly! */
  185. #define wrap(journal, var) \
  186. do { \
  187. if (var >= (journal)->j_last) \
  188. var -= ((journal)->j_last - (journal)->j_first); \
  189. } while (0)
  190. /**
  191. * jbd2_journal_recover - recovers a on-disk journal
  192. * @journal: the journal to recover
  193. *
  194. * The primary function for recovering the log contents when mounting a
  195. * journaled device.
  196. *
  197. * Recovery is done in three passes. In the first pass, we look for the
  198. * end of the log. In the second, we assemble the list of revoke
  199. * blocks. In the third and final pass, we replay any un-revoked blocks
  200. * in the log.
  201. */
  202. int jbd2_journal_recover(journal_t *journal)
  203. {
  204. int err, err2;
  205. journal_superblock_t * sb;
  206. struct recovery_info info;
  207. memset(&info, 0, sizeof(info));
  208. sb = journal->j_superblock;
  209. /*
  210. * The journal superblock's s_start field (the current log head)
  211. * is always zero if, and only if, the journal was cleanly
  212. * unmounted.
  213. */
  214. if (!sb->s_start) {
  215. jbd_debug(1, "No recovery required, last transaction %d\n",
  216. be32_to_cpu(sb->s_sequence));
  217. journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
  218. return 0;
  219. }
  220. err = do_one_pass(journal, &info, PASS_SCAN);
  221. if (!err)
  222. err = do_one_pass(journal, &info, PASS_REVOKE);
  223. if (!err)
  224. err = do_one_pass(journal, &info, PASS_REPLAY);
  225. jbd_debug(1, "JBD2: recovery, exit status %d, "
  226. "recovered transactions %u to %u\n",
  227. err, info.start_transaction, info.end_transaction);
  228. jbd_debug(1, "JBD2: Replayed %d and revoked %d/%d blocks\n",
  229. info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
  230. /* Restart the log at the next transaction ID, thus invalidating
  231. * any existing commit records in the log. */
  232. journal->j_transaction_sequence = ++info.end_transaction;
  233. jbd2_journal_clear_revoke(journal);
  234. err2 = sync_blockdev(journal->j_fs_dev);
  235. if (!err)
  236. err = err2;
  237. /* Make sure all replayed data is on permanent storage */
  238. if (journal->j_flags & JBD2_BARRIER) {
  239. err2 = blkdev_issue_flush(journal->j_fs_dev, GFP_KERNEL, NULL);
  240. if (!err)
  241. err = err2;
  242. }
  243. return err;
  244. }
  245. /**
  246. * jbd2_journal_skip_recovery - Start journal and wipe exiting records
  247. * @journal: journal to startup
  248. *
  249. * Locate any valid recovery information from the journal and set up the
  250. * journal structures in memory to ignore it (presumably because the
  251. * caller has evidence that it is out of date).
  252. * This function does'nt appear to be exorted..
  253. *
  254. * We perform one pass over the journal to allow us to tell the user how
  255. * much recovery information is being erased, and to let us initialise
  256. * the journal transaction sequence numbers to the next unused ID.
  257. */
  258. int jbd2_journal_skip_recovery(journal_t *journal)
  259. {
  260. int err;
  261. struct recovery_info info;
  262. memset (&info, 0, sizeof(info));
  263. err = do_one_pass(journal, &info, PASS_SCAN);
  264. if (err) {
  265. printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
  266. ++journal->j_transaction_sequence;
  267. } else {
  268. #ifdef CONFIG_JBD2_DEBUG
  269. int dropped = info.end_transaction -
  270. be32_to_cpu(journal->j_superblock->s_sequence);
  271. jbd_debug(1,
  272. "JBD2: ignoring %d transaction%s from the journal.\n",
  273. dropped, (dropped == 1) ? "" : "s");
  274. #endif
  275. journal->j_transaction_sequence = ++info.end_transaction;
  276. }
  277. journal->j_tail = 0;
  278. return err;
  279. }
  280. static inline unsigned long long read_tag_block(journal_t *journal,
  281. journal_block_tag_t *tag)
  282. {
  283. unsigned long long block = be32_to_cpu(tag->t_blocknr);
  284. if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
  285. block |= (u64)be32_to_cpu(tag->t_blocknr_high) << 32;
  286. return block;
  287. }
  288. /*
  289. * calc_chksums calculates the checksums for the blocks described in the
  290. * descriptor block.
  291. */
  292. static int calc_chksums(journal_t *journal, struct buffer_head *bh,
  293. unsigned long *next_log_block, __u32 *crc32_sum)
  294. {
  295. int i, num_blks, err;
  296. unsigned long io_block;
  297. struct buffer_head *obh;
  298. num_blks = count_tags(journal, bh);
  299. /* Calculate checksum of the descriptor block. */
  300. *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
  301. for (i = 0; i < num_blks; i++) {
  302. io_block = (*next_log_block)++;
  303. wrap(journal, *next_log_block);
  304. err = jread(&obh, journal, io_block);
  305. if (err) {
  306. printk(KERN_ERR "JBD2: IO error %d recovering block "
  307. "%lu in log\n", err, io_block);
  308. return 1;
  309. } else {
  310. *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
  311. obh->b_size);
  312. }
  313. put_bh(obh);
  314. }
  315. return 0;
  316. }
  317. static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
  318. {
  319. struct commit_header *h;
  320. __be32 provided;
  321. __u32 calculated;
  322. if (!jbd2_journal_has_csum_v2or3(j))
  323. return 1;
  324. h = buf;
  325. provided = h->h_chksum[0];
  326. h->h_chksum[0] = 0;
  327. calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
  328. h->h_chksum[0] = provided;
  329. return provided == cpu_to_be32(calculated);
  330. }
  331. static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
  332. void *buf, __u32 sequence)
  333. {
  334. journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
  335. __u32 csum32;
  336. __be32 seq;
  337. if (!jbd2_journal_has_csum_v2or3(j))
  338. return 1;
  339. seq = cpu_to_be32(sequence);
  340. csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
  341. csum32 = jbd2_chksum(j, csum32, buf, j->j_blocksize);
  342. if (JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V3))
  343. return tag3->t_checksum == cpu_to_be32(csum32);
  344. else
  345. return tag->t_checksum == cpu_to_be16(csum32);
  346. }
  347. static int do_one_pass(journal_t *journal,
  348. struct recovery_info *info, enum passtype pass)
  349. {
  350. unsigned int first_commit_ID, next_commit_ID;
  351. unsigned long next_log_block;
  352. int err, success = 0;
  353. journal_superblock_t * sb;
  354. journal_header_t * tmp;
  355. struct buffer_head * bh;
  356. unsigned int sequence;
  357. int blocktype;
  358. int tag_bytes = journal_tag_bytes(journal);
  359. __u32 crc32_sum = ~0; /* Transactional Checksums */
  360. int descr_csum_size = 0;
  361. int block_error = 0;
  362. /*
  363. * First thing is to establish what we expect to find in the log
  364. * (in terms of transaction IDs), and where (in terms of log
  365. * block offsets): query the superblock.
  366. */
  367. sb = journal->j_superblock;
  368. next_commit_ID = be32_to_cpu(sb->s_sequence);
  369. next_log_block = be32_to_cpu(sb->s_start);
  370. first_commit_ID = next_commit_ID;
  371. if (pass == PASS_SCAN)
  372. info->start_transaction = first_commit_ID;
  373. jbd_debug(1, "Starting recovery pass %d\n", pass);
  374. /*
  375. * Now we walk through the log, transaction by transaction,
  376. * making sure that each transaction has a commit block in the
  377. * expected place. Each complete transaction gets replayed back
  378. * into the main filesystem.
  379. */
  380. while (1) {
  381. int flags;
  382. char * tagp;
  383. journal_block_tag_t * tag;
  384. struct buffer_head * obh;
  385. struct buffer_head * nbh;
  386. cond_resched();
  387. /* If we already know where to stop the log traversal,
  388. * check right now that we haven't gone past the end of
  389. * the log. */
  390. if (pass != PASS_SCAN)
  391. if (tid_geq(next_commit_ID, info->end_transaction))
  392. break;
  393. jbd_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
  394. next_commit_ID, next_log_block, journal->j_last);
  395. /* Skip over each chunk of the transaction looking
  396. * either the next descriptor block or the final commit
  397. * record. */
  398. jbd_debug(3, "JBD2: checking block %ld\n", next_log_block);
  399. err = jread(&bh, journal, next_log_block);
  400. if (err)
  401. goto failed;
  402. next_log_block++;
  403. wrap(journal, next_log_block);
  404. /* What kind of buffer is it?
  405. *
  406. * If it is a descriptor block, check that it has the
  407. * expected sequence number. Otherwise, we're all done
  408. * here. */
  409. tmp = (journal_header_t *)bh->b_data;
  410. if (tmp->h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER)) {
  411. brelse(bh);
  412. break;
  413. }
  414. blocktype = be32_to_cpu(tmp->h_blocktype);
  415. sequence = be32_to_cpu(tmp->h_sequence);
  416. jbd_debug(3, "Found magic %d, sequence %d\n",
  417. blocktype, sequence);
  418. if (sequence != next_commit_ID) {
  419. brelse(bh);
  420. break;
  421. }
  422. /* OK, we have a valid descriptor block which matches
  423. * all of the sequence number checks. What are we going
  424. * to do with it? That depends on the pass... */
  425. switch(blocktype) {
  426. case JBD2_DESCRIPTOR_BLOCK:
  427. /* Verify checksum first */
  428. if (jbd2_journal_has_csum_v2or3(journal))
  429. descr_csum_size =
  430. sizeof(struct jbd2_journal_block_tail);
  431. if (descr_csum_size > 0 &&
  432. !jbd2_descr_block_csum_verify(journal,
  433. bh->b_data)) {
  434. printk(KERN_ERR "JBD2: Invalid checksum "
  435. "recovering block %lu in log\n",
  436. next_log_block);
  437. err = -EIO;
  438. brelse(bh);
  439. goto failed;
  440. }
  441. /* If it is a valid descriptor block, replay it
  442. * in pass REPLAY; if journal_checksums enabled, then
  443. * calculate checksums in PASS_SCAN, otherwise,
  444. * just skip over the blocks it describes. */
  445. if (pass != PASS_REPLAY) {
  446. if (pass == PASS_SCAN &&
  447. JBD2_HAS_COMPAT_FEATURE(journal,
  448. JBD2_FEATURE_COMPAT_CHECKSUM) &&
  449. !info->end_transaction) {
  450. if (calc_chksums(journal, bh,
  451. &next_log_block,
  452. &crc32_sum)) {
  453. put_bh(bh);
  454. break;
  455. }
  456. put_bh(bh);
  457. continue;
  458. }
  459. next_log_block += count_tags(journal, bh);
  460. wrap(journal, next_log_block);
  461. put_bh(bh);
  462. continue;
  463. }
  464. /* A descriptor block: we can now write all of
  465. * the data blocks. Yay, useful work is finally
  466. * getting done here! */
  467. tagp = &bh->b_data[sizeof(journal_header_t)];
  468. while ((tagp - bh->b_data + tag_bytes)
  469. <= journal->j_blocksize - descr_csum_size) {
  470. unsigned long io_block;
  471. tag = (journal_block_tag_t *) tagp;
  472. flags = be16_to_cpu(tag->t_flags);
  473. io_block = next_log_block++;
  474. wrap(journal, next_log_block);
  475. err = jread(&obh, journal, io_block);
  476. if (err) {
  477. /* Recover what we can, but
  478. * report failure at the end. */
  479. success = err;
  480. printk(KERN_ERR
  481. "JBD2: IO error %d recovering "
  482. "block %ld in log\n",
  483. err, io_block);
  484. } else {
  485. unsigned long long blocknr;
  486. J_ASSERT(obh != NULL);
  487. blocknr = read_tag_block(journal,
  488. tag);
  489. /* If the block has been
  490. * revoked, then we're all done
  491. * here. */
  492. if (jbd2_journal_test_revoke
  493. (journal, blocknr,
  494. next_commit_ID)) {
  495. brelse(obh);
  496. ++info->nr_revoke_hits;
  497. goto skip_write;
  498. }
  499. /* Look for block corruption */
  500. if (!jbd2_block_tag_csum_verify(
  501. journal, tag, obh->b_data,
  502. be32_to_cpu(tmp->h_sequence))) {
  503. brelse(obh);
  504. success = -EIO;
  505. printk(KERN_ERR "JBD2: Invalid "
  506. "checksum recovering "
  507. "block %llu in log\n",
  508. blocknr);
  509. block_error = 1;
  510. goto skip_write;
  511. }
  512. /* Find a buffer for the new
  513. * data being restored */
  514. nbh = __getblk(journal->j_fs_dev,
  515. blocknr,
  516. journal->j_blocksize);
  517. if (nbh == NULL) {
  518. printk(KERN_ERR
  519. "JBD2: Out of memory "
  520. "during recovery.\n");
  521. err = -ENOMEM;
  522. brelse(bh);
  523. brelse(obh);
  524. goto failed;
  525. }
  526. lock_buffer(nbh);
  527. memcpy(nbh->b_data, obh->b_data,
  528. journal->j_blocksize);
  529. if (flags & JBD2_FLAG_ESCAPE) {
  530. *((__be32 *)nbh->b_data) =
  531. cpu_to_be32(JBD2_MAGIC_NUMBER);
  532. }
  533. BUFFER_TRACE(nbh, "marking dirty");
  534. set_buffer_uptodate(nbh);
  535. mark_buffer_dirty(nbh);
  536. BUFFER_TRACE(nbh, "marking uptodate");
  537. ++info->nr_replays;
  538. /* ll_rw_block(WRITE, 1, &nbh); */
  539. unlock_buffer(nbh);
  540. brelse(obh);
  541. brelse(nbh);
  542. }
  543. skip_write:
  544. tagp += tag_bytes;
  545. if (!(flags & JBD2_FLAG_SAME_UUID))
  546. tagp += 16;
  547. if (flags & JBD2_FLAG_LAST_TAG)
  548. break;
  549. }
  550. brelse(bh);
  551. continue;
  552. case JBD2_COMMIT_BLOCK:
  553. /* How to differentiate between interrupted commit
  554. * and journal corruption ?
  555. *
  556. * {nth transaction}
  557. * Checksum Verification Failed
  558. * |
  559. * ____________________
  560. * | |
  561. * async_commit sync_commit
  562. * | |
  563. * | GO TO NEXT "Journal Corruption"
  564. * | TRANSACTION
  565. * |
  566. * {(n+1)th transanction}
  567. * |
  568. * _______|______________
  569. * | |
  570. * Commit block found Commit block not found
  571. * | |
  572. * "Journal Corruption" |
  573. * _____________|_________
  574. * | |
  575. * nth trans corrupt OR nth trans
  576. * and (n+1)th interrupted interrupted
  577. * before commit block
  578. * could reach the disk.
  579. * (Cannot find the difference in above
  580. * mentioned conditions. Hence assume
  581. * "Interrupted Commit".)
  582. */
  583. /* Found an expected commit block: if checksums
  584. * are present verify them in PASS_SCAN; else not
  585. * much to do other than move on to the next sequence
  586. * number. */
  587. if (pass == PASS_SCAN &&
  588. JBD2_HAS_COMPAT_FEATURE(journal,
  589. JBD2_FEATURE_COMPAT_CHECKSUM)) {
  590. int chksum_err, chksum_seen;
  591. struct commit_header *cbh =
  592. (struct commit_header *)bh->b_data;
  593. unsigned found_chksum =
  594. be32_to_cpu(cbh->h_chksum[0]);
  595. chksum_err = chksum_seen = 0;
  596. if (info->end_transaction) {
  597. journal->j_failed_commit =
  598. info->end_transaction;
  599. brelse(bh);
  600. break;
  601. }
  602. if (crc32_sum == found_chksum &&
  603. cbh->h_chksum_type == JBD2_CRC32_CHKSUM &&
  604. cbh->h_chksum_size ==
  605. JBD2_CRC32_CHKSUM_SIZE)
  606. chksum_seen = 1;
  607. else if (!(cbh->h_chksum_type == 0 &&
  608. cbh->h_chksum_size == 0 &&
  609. found_chksum == 0 &&
  610. !chksum_seen))
  611. /*
  612. * If fs is mounted using an old kernel and then
  613. * kernel with journal_chksum is used then we
  614. * get a situation where the journal flag has
  615. * checksum flag set but checksums are not
  616. * present i.e chksum = 0, in the individual
  617. * commit blocks.
  618. * Hence to avoid checksum failures, in this
  619. * situation, this extra check is added.
  620. */
  621. chksum_err = 1;
  622. if (chksum_err) {
  623. info->end_transaction = next_commit_ID;
  624. if (!JBD2_HAS_INCOMPAT_FEATURE(journal,
  625. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)){
  626. journal->j_failed_commit =
  627. next_commit_ID;
  628. brelse(bh);
  629. break;
  630. }
  631. }
  632. crc32_sum = ~0;
  633. }
  634. if (pass == PASS_SCAN &&
  635. !jbd2_commit_block_csum_verify(journal,
  636. bh->b_data)) {
  637. info->end_transaction = next_commit_ID;
  638. if (!JBD2_HAS_INCOMPAT_FEATURE(journal,
  639. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
  640. journal->j_failed_commit =
  641. next_commit_ID;
  642. brelse(bh);
  643. break;
  644. }
  645. }
  646. brelse(bh);
  647. next_commit_ID++;
  648. continue;
  649. case JBD2_REVOKE_BLOCK:
  650. /* If we aren't in the REVOKE pass, then we can
  651. * just skip over this block. */
  652. if (pass != PASS_REVOKE) {
  653. brelse(bh);
  654. continue;
  655. }
  656. err = scan_revoke_records(journal, bh,
  657. next_commit_ID, info);
  658. brelse(bh);
  659. if (err)
  660. goto failed;
  661. continue;
  662. default:
  663. jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
  664. blocktype);
  665. brelse(bh);
  666. goto done;
  667. }
  668. }
  669. done:
  670. /*
  671. * We broke out of the log scan loop: either we came to the
  672. * known end of the log or we found an unexpected block in the
  673. * log. If the latter happened, then we know that the "current"
  674. * transaction marks the end of the valid log.
  675. */
  676. if (pass == PASS_SCAN) {
  677. if (!info->end_transaction)
  678. info->end_transaction = next_commit_ID;
  679. } else {
  680. /* It's really bad news if different passes end up at
  681. * different places (but possible due to IO errors). */
  682. if (info->end_transaction != next_commit_ID) {
  683. printk(KERN_ERR "JBD2: recovery pass %d ended at "
  684. "transaction %u, expected %u\n",
  685. pass, next_commit_ID, info->end_transaction);
  686. if (!success)
  687. success = -EIO;
  688. }
  689. }
  690. if (block_error && success == 0)
  691. success = -EIO;
  692. return success;
  693. failed:
  694. return err;
  695. }
  696. static int jbd2_revoke_block_csum_verify(journal_t *j,
  697. void *buf)
  698. {
  699. struct jbd2_journal_revoke_tail *tail;
  700. __be32 provided;
  701. __u32 calculated;
  702. if (!jbd2_journal_has_csum_v2or3(j))
  703. return 1;
  704. tail = (struct jbd2_journal_revoke_tail *)(buf + j->j_blocksize -
  705. sizeof(struct jbd2_journal_revoke_tail));
  706. provided = tail->r_checksum;
  707. tail->r_checksum = 0;
  708. calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
  709. tail->r_checksum = provided;
  710. return provided == cpu_to_be32(calculated);
  711. }
  712. /* Scan a revoke record, marking all blocks mentioned as revoked. */
  713. static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
  714. tid_t sequence, struct recovery_info *info)
  715. {
  716. jbd2_journal_revoke_header_t *header;
  717. int offset, max;
  718. int csum_size = 0;
  719. __u32 rcount;
  720. int record_len = 4;
  721. header = (jbd2_journal_revoke_header_t *) bh->b_data;
  722. offset = sizeof(jbd2_journal_revoke_header_t);
  723. rcount = be32_to_cpu(header->r_count);
  724. if (!jbd2_revoke_block_csum_verify(journal, header))
  725. return -EINVAL;
  726. if (jbd2_journal_has_csum_v2or3(journal))
  727. csum_size = sizeof(struct jbd2_journal_revoke_tail);
  728. if (rcount > journal->j_blocksize - csum_size)
  729. return -EINVAL;
  730. max = rcount;
  731. if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
  732. record_len = 8;
  733. while (offset + record_len <= max) {
  734. unsigned long long blocknr;
  735. int err;
  736. if (record_len == 4)
  737. blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
  738. else
  739. blocknr = be64_to_cpu(* ((__be64 *) (bh->b_data+offset)));
  740. offset += record_len;
  741. err = jbd2_journal_set_revoke(journal, blocknr, sequence);
  742. if (err)
  743. return err;
  744. ++info->nr_revokes;
  745. }
  746. return 0;
  747. }