1 // SPDX-License-Identifier: GPL-2.0+
3 * linux/fs/jbd2/recovery.c
5 * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
7 * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
9 * Journal recovery routines for the generic filesystem journaling code;
10 * part of the ext2fs journaling system.
16 #include <linux/time.h>
18 #include <linux/jbd2.h>
19 #include <linux/errno.h>
20 #include <linux/crc32.h>
21 #include <linux/blkdev.h>
25 * Maintain information about the progress of the recovery job, so that
26 * the different passes can carry information between them.
30 tid_t start_transaction;
31 tid_t end_transaction;
32 unsigned long head_block;
39 static int do_one_pass(journal_t *journal,
40 struct recovery_info *info, enum passtype pass);
41 static int scan_revoke_records(journal_t *, struct buffer_head *,
42 tid_t, struct recovery_info *);
46 /* Release readahead buffers after use */
47 static void journal_brelse_array(struct buffer_head *b[], int n)
55 * When reading from the journal, we are going through the block device
56 * layer directly and so there is no readahead being done for us. We
57 * need to implement any readahead ourselves if we want it to happen at
58 * all. Recovery is basically one long sequential read, so make sure we
59 * do the IO in reasonably large chunks.
61 * This is not so critical that we need to be enormously clever about
62 * the readahead size, though. 128K is a purely arbitrary, good-enough
67 static int do_readahead(journal_t *journal, unsigned int start)
70 unsigned int max, nbufs, next;
71 unsigned long long blocknr;
72 struct buffer_head *bh;
74 struct buffer_head * bufs[MAXBUF];
76 /* Do up to 128K of readahead */
77 max = start + (128 * 1024 / journal->j_blocksize);
78 if (max > journal->j_total_len)
79 max = journal->j_total_len;
81 /* Do the readahead itself. We'll submit MAXBUF buffer_heads at
82 * a time to the block device IO layer. */
86 for (next = start; next < max; next++) {
87 err = jbd2_journal_bmap(journal, next, &blocknr);
90 printk(KERN_ERR "JBD2: bad block at offset %u\n",
95 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
101 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
103 if (nbufs == MAXBUF) {
104 bh_readahead_batch(nbufs, bufs, 0);
105 journal_brelse_array(bufs, nbufs);
113 bh_readahead_batch(nbufs, bufs, 0);
118 journal_brelse_array(bufs, nbufs);
122 #endif /* __KERNEL__ */
126 * Read a block from the journal
129 static int jread(struct buffer_head **bhp, journal_t *journal,
133 unsigned long long blocknr;
134 struct buffer_head *bh;
138 if (offset >= journal->j_total_len) {
139 printk(KERN_ERR "JBD2: corrupted journal superblock\n");
140 return -EFSCORRUPTED;
143 err = jbd2_journal_bmap(journal, offset, &blocknr);
146 printk(KERN_ERR "JBD2: bad block at offset %u\n",
151 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
155 if (!buffer_uptodate(bh)) {
157 * If this is a brand new buffer, start readahead.
158 * Otherwise, we assume we are already reading it.
160 bool need_readahead = !buffer_req(bh);
162 bh_read_nowait(bh, 0);
164 do_readahead(journal, offset);
168 if (!buffer_uptodate(bh)) {
169 printk(KERN_ERR "JBD2: Failed to read block at offset %u\n",
179 static int jbd2_descriptor_block_csum_verify(journal_t *j, void *buf)
181 struct jbd2_journal_block_tail *tail;
185 if (!jbd2_journal_has_csum_v2or3(j))
188 tail = (struct jbd2_journal_block_tail *)((char *)buf +
189 j->j_blocksize - sizeof(struct jbd2_journal_block_tail));
190 provided = tail->t_checksum;
191 tail->t_checksum = 0;
192 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
193 tail->t_checksum = provided;
195 return provided == cpu_to_be32(calculated);
199 * Count the number of in-use tags in a journal descriptor block.
202 static int count_tags(journal_t *journal, struct buffer_head *bh)
205 journal_block_tag_t tag;
206 int nr = 0, size = journal->j_blocksize;
207 int tag_bytes = journal_tag_bytes(journal);
209 if (jbd2_journal_has_csum_v2or3(journal))
210 size -= sizeof(struct jbd2_journal_block_tail);
212 tagp = &bh->b_data[sizeof(journal_header_t)];
214 while ((tagp - bh->b_data + tag_bytes) <= size) {
215 memcpy(&tag, tagp, sizeof(tag));
219 if (!(tag.t_flags & cpu_to_be16(JBD2_FLAG_SAME_UUID)))
222 if (tag.t_flags & cpu_to_be16(JBD2_FLAG_LAST_TAG))
230 /* Make sure we wrap around the log correctly! */
231 #define wrap(journal, var) \
233 unsigned long _wrap_last = \
234 jbd2_has_feature_fast_commit(journal) ? \
235 (journal)->j_fc_last : (journal)->j_last; \
237 if (var >= _wrap_last) \
238 var -= (_wrap_last - (journal)->j_first); \
241 static int fc_do_one_pass(journal_t *journal,
242 struct recovery_info *info, enum passtype pass)
244 unsigned int expected_commit_id = info->end_transaction;
245 unsigned long next_fc_block;
246 struct buffer_head *bh;
249 next_fc_block = journal->j_fc_first;
250 if (!journal->j_fc_replay_callback)
253 while (next_fc_block <= journal->j_fc_last) {
254 jbd2_debug(3, "Fast commit replay: next block %ld\n",
256 err = jread(&bh, journal, next_fc_block);
258 jbd2_debug(3, "Fast commit replay: read error\n");
262 err = journal->j_fc_replay_callback(journal, bh, pass,
263 next_fc_block - journal->j_fc_first,
267 if (err < 0 || err == JBD2_FC_REPLAY_STOP)
273 jbd2_debug(3, "Fast commit replay failed, err = %d\n", err);
279 * jbd2_journal_recover - recovers a on-disk journal
280 * @journal: the journal to recover
282 * The primary function for recovering the log contents when mounting a
285 * Recovery is done in three passes. In the first pass, we look for the
286 * end of the log. In the second, we assemble the list of revoke
287 * blocks. In the third and final pass, we replay any un-revoked blocks
290 int jbd2_journal_recover(journal_t *journal)
293 journal_superblock_t * sb;
295 struct recovery_info info;
297 memset(&info, 0, sizeof(info));
298 sb = journal->j_superblock;
301 * The journal superblock's s_start field (the current log head)
302 * is always zero if, and only if, the journal was cleanly
306 jbd2_debug(1, "No recovery required, last transaction %d, head block %u\n",
307 be32_to_cpu(sb->s_sequence), be32_to_cpu(sb->s_head));
308 journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
309 journal->j_head = be32_to_cpu(sb->s_head);
313 err = do_one_pass(journal, &info, PASS_SCAN);
315 err = do_one_pass(journal, &info, PASS_REVOKE);
317 err = do_one_pass(journal, &info, PASS_REPLAY);
319 jbd2_debug(1, "JBD2: recovery, exit status %d, "
320 "recovered transactions %u to %u\n",
321 err, info.start_transaction, info.end_transaction);
322 jbd2_debug(1, "JBD2: Replayed %d and revoked %d/%d blocks\n",
323 info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
325 /* Restart the log at the next transaction ID, thus invalidating
326 * any existing commit records in the log. */
327 journal->j_transaction_sequence = ++info.end_transaction;
328 journal->j_head = info.head_block;
329 jbd2_debug(1, "JBD2: last transaction %d, head block %lu\n",
330 journal->j_transaction_sequence, journal->j_head);
332 jbd2_journal_clear_revoke(journal);
333 err2 = sync_blockdev(journal->j_fs_dev);
336 /* Make sure all replayed data is on permanent storage */
337 if (journal->j_flags & JBD2_BARRIER) {
338 err2 = blkdev_issue_flush(journal->j_fs_dev);
346 * jbd2_journal_skip_recovery - Start journal and wipe exiting records
347 * @journal: journal to startup
349 * Locate any valid recovery information from the journal and set up the
350 * journal structures in memory to ignore it (presumably because the
351 * caller has evidence that it is out of date).
352 * This function doesn't appear to be exported..
354 * We perform one pass over the journal to allow us to tell the user how
355 * much recovery information is being erased, and to let us initialise
356 * the journal transaction sequence numbers to the next unused ID.
358 int jbd2_journal_skip_recovery(journal_t *journal)
362 struct recovery_info info;
364 memset (&info, 0, sizeof(info));
366 err = do_one_pass(journal, &info, PASS_SCAN);
369 printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
370 ++journal->j_transaction_sequence;
371 journal->j_head = journal->j_first;
373 #ifdef CONFIG_JBD2_DEBUG
374 int dropped = info.end_transaction -
375 be32_to_cpu(journal->j_superblock->s_sequence);
377 "JBD2: ignoring %d transaction%s from the journal.\n",
378 dropped, (dropped == 1) ? "" : "s");
380 journal->j_transaction_sequence = ++info.end_transaction;
381 journal->j_head = info.head_block;
388 static inline unsigned long long read_tag_block(journal_t *journal,
389 journal_block_tag_t *tag)
391 unsigned long long block = be32_to_cpu(tag->t_blocknr);
392 if (jbd2_has_feature_64bit(journal))
393 block |= (u64)be32_to_cpu(tag->t_blocknr_high) << 32;
398 * calc_chksums calculates the checksums for the blocks described in the
401 static int calc_chksums(journal_t *journal, struct buffer_head *bh,
402 unsigned long *next_log_block, __u32 *crc32_sum)
404 int i, num_blks, err;
405 unsigned long io_block;
406 struct buffer_head *obh;
408 num_blks = count_tags(journal, bh);
409 /* Calculate checksum of the descriptor block. */
410 *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
412 for (i = 0; i < num_blks; i++) {
413 io_block = (*next_log_block)++;
414 wrap(journal, *next_log_block);
415 err = jread(&obh, journal, io_block);
417 printk(KERN_ERR "JBD2: IO error %d recovering block "
418 "%lu in log\n", err, io_block);
421 *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
429 static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
431 struct commit_header *h;
435 if (!jbd2_journal_has_csum_v2or3(j))
439 provided = h->h_chksum[0];
441 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
442 h->h_chksum[0] = provided;
444 return provided == cpu_to_be32(calculated);
447 static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
448 journal_block_tag3_t *tag3,
449 void *buf, __u32 sequence)
454 if (!jbd2_journal_has_csum_v2or3(j))
457 seq = cpu_to_be32(sequence);
458 csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
459 csum32 = jbd2_chksum(j, csum32, buf, j->j_blocksize);
461 if (jbd2_has_feature_csum3(j))
462 return tag3->t_checksum == cpu_to_be32(csum32);
464 return tag->t_checksum == cpu_to_be16(csum32);
467 static int do_one_pass(journal_t *journal,
468 struct recovery_info *info, enum passtype pass)
470 unsigned int first_commit_ID, next_commit_ID;
471 unsigned long next_log_block, head_block;
472 int err, success = 0;
473 journal_superblock_t * sb;
474 journal_header_t * tmp;
475 struct buffer_head * bh;
476 unsigned int sequence;
478 int tag_bytes = journal_tag_bytes(journal);
479 __u32 crc32_sum = ~0; /* Transactional Checksums */
480 int descr_csum_size = 0;
482 bool need_check_commit_time = false;
483 __u64 last_trans_commit_time = 0, commit_time;
486 * First thing is to establish what we expect to find in the log
487 * (in terms of transaction IDs), and where (in terms of log
488 * block offsets): query the superblock.
491 sb = journal->j_superblock;
492 next_commit_ID = be32_to_cpu(sb->s_sequence);
493 next_log_block = be32_to_cpu(sb->s_start);
494 head_block = next_log_block;
496 first_commit_ID = next_commit_ID;
497 if (pass == PASS_SCAN)
498 info->start_transaction = first_commit_ID;
500 jbd2_debug(1, "Starting recovery pass %d\n", pass);
503 * Now we walk through the log, transaction by transaction,
504 * making sure that each transaction has a commit block in the
505 * expected place. Each complete transaction gets replayed back
506 * into the main filesystem.
512 journal_block_tag_t tag;
513 struct buffer_head * obh;
514 struct buffer_head * nbh;
518 /* If we already know where to stop the log traversal,
519 * check right now that we haven't gone past the end of
522 if (pass != PASS_SCAN)
523 if (tid_geq(next_commit_ID, info->end_transaction))
526 jbd2_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
527 next_commit_ID, next_log_block,
528 jbd2_has_feature_fast_commit(journal) ?
529 journal->j_fc_last : journal->j_last);
531 /* Skip over each chunk of the transaction looking
532 * either the next descriptor block or the final commit
535 jbd2_debug(3, "JBD2: checking block %ld\n", next_log_block);
536 err = jread(&bh, journal, next_log_block);
541 wrap(journal, next_log_block);
543 /* What kind of buffer is it?
545 * If it is a descriptor block, check that it has the
546 * expected sequence number. Otherwise, we're all done
549 tmp = (journal_header_t *)bh->b_data;
551 if (tmp->h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER)) {
556 blocktype = be32_to_cpu(tmp->h_blocktype);
557 sequence = be32_to_cpu(tmp->h_sequence);
558 jbd2_debug(3, "Found magic %d, sequence %d\n",
559 blocktype, sequence);
561 if (sequence != next_commit_ID) {
566 /* OK, we have a valid descriptor block which matches
567 * all of the sequence number checks. What are we going
568 * to do with it? That depends on the pass... */
571 case JBD2_DESCRIPTOR_BLOCK:
572 /* Verify checksum first */
573 if (jbd2_journal_has_csum_v2or3(journal))
575 sizeof(struct jbd2_journal_block_tail);
576 if (descr_csum_size > 0 &&
577 !jbd2_descriptor_block_csum_verify(journal,
580 * PASS_SCAN can see stale blocks due to lazy
581 * journal init. Don't error out on those yet.
583 if (pass != PASS_SCAN) {
584 pr_err("JBD2: Invalid checksum recovering block %lu in log\n",
590 need_check_commit_time = true;
592 "invalid descriptor block found in %lu\n",
596 /* If it is a valid descriptor block, replay it
597 * in pass REPLAY; if journal_checksums enabled, then
598 * calculate checksums in PASS_SCAN, otherwise,
599 * just skip over the blocks it describes. */
600 if (pass != PASS_REPLAY) {
601 if (pass == PASS_SCAN &&
602 jbd2_has_feature_checksum(journal) &&
603 !need_check_commit_time &&
604 !info->end_transaction) {
605 if (calc_chksums(journal, bh,
614 next_log_block += count_tags(journal, bh);
615 wrap(journal, next_log_block);
620 /* A descriptor block: we can now write all of
621 * the data blocks. Yay, useful work is finally
622 * getting done here! */
624 tagp = &bh->b_data[sizeof(journal_header_t)];
625 while ((tagp - bh->b_data + tag_bytes)
626 <= journal->j_blocksize - descr_csum_size) {
627 unsigned long io_block;
629 memcpy(&tag, tagp, sizeof(tag));
630 flags = be16_to_cpu(tag.t_flags);
632 io_block = next_log_block++;
633 wrap(journal, next_log_block);
634 err = jread(&obh, journal, io_block);
636 /* Recover what we can, but
637 * report failure at the end. */
640 "JBD2: IO error %d recovering "
641 "block %ld in log\n",
644 unsigned long long blocknr;
646 J_ASSERT(obh != NULL);
647 blocknr = read_tag_block(journal,
650 /* If the block has been
651 * revoked, then we're all done
653 if (jbd2_journal_test_revoke
657 ++info->nr_revoke_hits;
661 /* Look for block corruption */
662 if (!jbd2_block_tag_csum_verify(
663 journal, &tag, (journal_block_tag3_t *)tagp,
664 obh->b_data, be32_to_cpu(tmp->h_sequence))) {
666 success = -EFSBADCRC;
667 printk(KERN_ERR "JBD2: Invalid "
668 "checksum recovering "
669 "data block %llu in "
675 /* Find a buffer for the new
676 * data being restored */
677 nbh = __getblk(journal->j_fs_dev,
679 journal->j_blocksize);
682 "JBD2: Out of memory "
683 "during recovery.\n");
691 memcpy(nbh->b_data, obh->b_data,
692 journal->j_blocksize);
693 if (flags & JBD2_FLAG_ESCAPE) {
694 *((__be32 *)nbh->b_data) =
695 cpu_to_be32(JBD2_MAGIC_NUMBER);
698 BUFFER_TRACE(nbh, "marking dirty");
699 set_buffer_uptodate(nbh);
700 mark_buffer_dirty(nbh);
701 BUFFER_TRACE(nbh, "marking uptodate");
710 if (!(flags & JBD2_FLAG_SAME_UUID))
713 if (flags & JBD2_FLAG_LAST_TAG)
720 case JBD2_COMMIT_BLOCK:
721 /* How to differentiate between interrupted commit
722 * and journal corruption ?
725 * Checksum Verification Failed
727 * ____________________
729 * async_commit sync_commit
731 * | GO TO NEXT "Journal Corruption"
734 * {(n+1)th transanction}
736 * _______|______________
738 * Commit block found Commit block not found
740 * "Journal Corruption" |
741 * _____________|_________
743 * nth trans corrupt OR nth trans
744 * and (n+1)th interrupted interrupted
745 * before commit block
746 * could reach the disk.
747 * (Cannot find the difference in above
748 * mentioned conditions. Hence assume
749 * "Interrupted Commit".)
751 commit_time = be64_to_cpu(
752 ((struct commit_header *)bh->b_data)->h_commit_sec);
754 * If need_check_commit_time is set, it means we are in
755 * PASS_SCAN and csum verify failed before. If
756 * commit_time is increasing, it's the same journal,
757 * otherwise it is stale journal block, just end this
760 if (need_check_commit_time) {
761 if (commit_time >= last_trans_commit_time) {
762 pr_err("JBD2: Invalid checksum found in transaction %u\n",
770 * It likely does not belong to same journal,
771 * just end this recovery with success.
773 jbd2_debug(1, "JBD2: Invalid checksum ignored in transaction %u, likely stale data\n",
780 * Found an expected commit block: if checksums
781 * are present, verify them in PASS_SCAN; else not
782 * much to do other than move on to the next sequence
785 if (pass == PASS_SCAN &&
786 jbd2_has_feature_checksum(journal)) {
787 struct commit_header *cbh =
788 (struct commit_header *)bh->b_data;
789 unsigned found_chksum =
790 be32_to_cpu(cbh->h_chksum[0]);
792 if (info->end_transaction) {
793 journal->j_failed_commit =
794 info->end_transaction;
799 /* Neither checksum match nor unused? */
800 if (!((crc32_sum == found_chksum &&
801 cbh->h_chksum_type ==
803 cbh->h_chksum_size ==
804 JBD2_CRC32_CHKSUM_SIZE) ||
805 (cbh->h_chksum_type == 0 &&
806 cbh->h_chksum_size == 0 &&
812 if (pass == PASS_SCAN &&
813 !jbd2_commit_block_csum_verify(journal,
816 if (commit_time < last_trans_commit_time)
817 goto ignore_crc_mismatch;
818 info->end_transaction = next_commit_ID;
819 info->head_block = head_block;
821 if (!jbd2_has_feature_async_commit(journal)) {
822 journal->j_failed_commit =
828 if (pass == PASS_SCAN) {
829 last_trans_commit_time = commit_time;
830 head_block = next_log_block;
836 case JBD2_REVOKE_BLOCK:
838 * Check revoke block crc in pass_scan, if csum verify
839 * failed, check commit block time later.
841 if (pass == PASS_SCAN &&
842 !jbd2_descriptor_block_csum_verify(journal,
844 jbd2_debug(1, "JBD2: invalid revoke block found in %lu\n",
846 need_check_commit_time = true;
848 /* If we aren't in the REVOKE pass, then we can
849 * just skip over this block. */
850 if (pass != PASS_REVOKE) {
855 err = scan_revoke_records(journal, bh,
856 next_commit_ID, info);
863 jbd2_debug(3, "Unrecognised magic %d, end of scan.\n",
872 * We broke out of the log scan loop: either we came to the
873 * known end of the log or we found an unexpected block in the
874 * log. If the latter happened, then we know that the "current"
875 * transaction marks the end of the valid log.
878 if (pass == PASS_SCAN) {
879 if (!info->end_transaction)
880 info->end_transaction = next_commit_ID;
881 if (!info->head_block)
882 info->head_block = head_block;
884 /* It's really bad news if different passes end up at
885 * different places (but possible due to IO errors). */
886 if (info->end_transaction != next_commit_ID) {
887 printk(KERN_ERR "JBD2: recovery pass %d ended at "
888 "transaction %u, expected %u\n",
889 pass, next_commit_ID, info->end_transaction);
895 if (jbd2_has_feature_fast_commit(journal) && pass != PASS_REVOKE) {
896 err = fc_do_one_pass(journal, info, pass);
901 if (block_error && success == 0)
909 /* Scan a revoke record, marking all blocks mentioned as revoked. */
911 static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
912 tid_t sequence, struct recovery_info *info)
914 jbd2_journal_revoke_header_t *header;
916 unsigned csum_size = 0;
920 header = (jbd2_journal_revoke_header_t *) bh->b_data;
921 offset = sizeof(jbd2_journal_revoke_header_t);
922 rcount = be32_to_cpu(header->r_count);
924 if (jbd2_journal_has_csum_v2or3(journal))
925 csum_size = sizeof(struct jbd2_journal_block_tail);
926 if (rcount > journal->j_blocksize - csum_size)
930 if (jbd2_has_feature_64bit(journal))
933 while (offset + record_len <= max) {
934 unsigned long long blocknr;
938 blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
940 blocknr = be64_to_cpu(* ((__be64 *) (bh->b_data+offset)));
941 offset += record_len;
942 err = jbd2_journal_set_revoke(journal, blocknr, sequence);