recovery.c 25 KB

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