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;
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 *);
45 /* Release readahead buffers after use */
46 static void journal_brelse_array(struct buffer_head *b[], int n)
54 * When reading from the journal, we are going through the block device
55 * layer directly and so there is no readahead being done for us. We
56 * need to implement any readahead ourselves if we want it to happen at
57 * all. Recovery is basically one long sequential read, so make sure we
58 * do the IO in reasonably large chunks.
60 * This is not so critical that we need to be enormously clever about
61 * the readahead size, though. 128K is a purely arbitrary, good-enough
66 static int do_readahead(journal_t *journal, unsigned int start)
69 unsigned int max, nbufs, next;
70 unsigned long long blocknr;
71 struct buffer_head *bh;
73 struct buffer_head * bufs[MAXBUF];
75 /* Do up to 128K of readahead */
76 max = start + (128 * 1024 / journal->j_blocksize);
77 if (max > journal->j_total_len)
78 max = journal->j_total_len;
80 /* Do the readahead itself. We'll submit MAXBUF buffer_heads at
81 * a time to the block device IO layer. */
85 for (next = start; next < max; next++) {
86 err = jbd2_journal_bmap(journal, next, &blocknr);
89 printk(KERN_ERR "JBD2: bad block at offset %u\n",
94 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
100 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
102 if (nbufs == MAXBUF) {
103 bh_readahead_batch(nbufs, bufs, 0);
104 journal_brelse_array(bufs, nbufs);
112 bh_readahead_batch(nbufs, bufs, 0);
117 journal_brelse_array(bufs, nbufs);
121 #endif /* __KERNEL__ */
125 * Read a block from the journal
128 static int jread(struct buffer_head **bhp, journal_t *journal,
132 unsigned long long blocknr;
133 struct buffer_head *bh;
137 if (offset >= journal->j_total_len) {
138 printk(KERN_ERR "JBD2: corrupted journal superblock\n");
139 return -EFSCORRUPTED;
142 err = jbd2_journal_bmap(journal, offset, &blocknr);
145 printk(KERN_ERR "JBD2: bad block at offset %u\n",
150 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
154 if (!buffer_uptodate(bh)) {
156 * If this is a brand new buffer, start readahead.
157 * Otherwise, we assume we are already reading it.
159 bool need_readahead = !buffer_req(bh);
161 bh_read_nowait(bh, 0);
163 do_readahead(journal, offset);
167 if (!buffer_uptodate(bh)) {
168 printk(KERN_ERR "JBD2: Failed to read block at offset %u\n",
178 static int jbd2_descriptor_block_csum_verify(journal_t *j, void *buf)
180 struct jbd2_journal_block_tail *tail;
184 if (!jbd2_journal_has_csum_v2or3(j))
187 tail = (struct jbd2_journal_block_tail *)((char *)buf +
188 j->j_blocksize - sizeof(struct jbd2_journal_block_tail));
189 provided = tail->t_checksum;
190 tail->t_checksum = 0;
191 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
192 tail->t_checksum = provided;
194 return provided == cpu_to_be32(calculated);
198 * Count the number of in-use tags in a journal descriptor block.
201 static int count_tags(journal_t *journal, struct buffer_head *bh)
204 journal_block_tag_t tag;
205 int nr = 0, size = journal->j_blocksize;
206 int tag_bytes = journal_tag_bytes(journal);
208 if (jbd2_journal_has_csum_v2or3(journal))
209 size -= sizeof(struct jbd2_journal_block_tail);
211 tagp = &bh->b_data[sizeof(journal_header_t)];
213 while ((tagp - bh->b_data + tag_bytes) <= size) {
214 memcpy(&tag, tagp, sizeof(tag));
218 if (!(tag.t_flags & cpu_to_be16(JBD2_FLAG_SAME_UUID)))
221 if (tag.t_flags & cpu_to_be16(JBD2_FLAG_LAST_TAG))
229 /* Make sure we wrap around the log correctly! */
230 #define wrap(journal, var) \
232 unsigned long _wrap_last = \
233 jbd2_has_feature_fast_commit(journal) ? \
234 (journal)->j_fc_last : (journal)->j_last; \
236 if (var >= _wrap_last) \
237 var -= (_wrap_last - (journal)->j_first); \
240 static int fc_do_one_pass(journal_t *journal,
241 struct recovery_info *info, enum passtype pass)
243 unsigned int expected_commit_id = info->end_transaction;
244 unsigned long next_fc_block;
245 struct buffer_head *bh;
248 next_fc_block = journal->j_fc_first;
249 if (!journal->j_fc_replay_callback)
252 while (next_fc_block <= journal->j_fc_last) {
253 jbd2_debug(3, "Fast commit replay: next block %ld\n",
255 err = jread(&bh, journal, next_fc_block);
257 jbd2_debug(3, "Fast commit replay: read error\n");
261 err = journal->j_fc_replay_callback(journal, bh, pass,
262 next_fc_block - journal->j_fc_first,
266 if (err < 0 || err == JBD2_FC_REPLAY_STOP)
272 jbd2_debug(3, "Fast commit replay failed, err = %d\n", err);
278 * jbd2_journal_recover - recovers a on-disk journal
279 * @journal: the journal to recover
281 * The primary function for recovering the log contents when mounting a
284 * Recovery is done in three passes. In the first pass, we look for the
285 * end of the log. In the second, we assemble the list of revoke
286 * blocks. In the third and final pass, we replay any un-revoked blocks
289 int jbd2_journal_recover(journal_t *journal)
292 journal_superblock_t * sb;
294 struct recovery_info info;
296 memset(&info, 0, sizeof(info));
297 sb = journal->j_superblock;
300 * The journal superblock's s_start field (the current log head)
301 * is always zero if, and only if, the journal was cleanly
306 jbd2_debug(1, "No recovery required, last transaction %d\n",
307 be32_to_cpu(sb->s_sequence));
308 journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
312 err = do_one_pass(journal, &info, PASS_SCAN);
314 err = do_one_pass(journal, &info, PASS_REVOKE);
316 err = do_one_pass(journal, &info, PASS_REPLAY);
318 jbd2_debug(1, "JBD2: recovery, exit status %d, "
319 "recovered transactions %u to %u\n",
320 err, info.start_transaction, info.end_transaction);
321 jbd2_debug(1, "JBD2: Replayed %d and revoked %d/%d blocks\n",
322 info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
324 /* Restart the log at the next transaction ID, thus invalidating
325 * any existing commit records in the log. */
326 journal->j_transaction_sequence = ++info.end_transaction;
328 jbd2_journal_clear_revoke(journal);
329 err2 = sync_blockdev(journal->j_fs_dev);
332 /* Make sure all replayed data is on permanent storage */
333 if (journal->j_flags & JBD2_BARRIER) {
334 err2 = blkdev_issue_flush(journal->j_fs_dev);
342 * jbd2_journal_skip_recovery - Start journal and wipe exiting records
343 * @journal: journal to startup
345 * Locate any valid recovery information from the journal and set up the
346 * journal structures in memory to ignore it (presumably because the
347 * caller has evidence that it is out of date).
348 * This function doesn't appear to be exported..
350 * We perform one pass over the journal to allow us to tell the user how
351 * much recovery information is being erased, and to let us initialise
352 * the journal transaction sequence numbers to the next unused ID.
354 int jbd2_journal_skip_recovery(journal_t *journal)
358 struct recovery_info info;
360 memset (&info, 0, sizeof(info));
362 err = do_one_pass(journal, &info, PASS_SCAN);
365 printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
366 ++journal->j_transaction_sequence;
368 #ifdef CONFIG_JBD2_DEBUG
369 int dropped = info.end_transaction -
370 be32_to_cpu(journal->j_superblock->s_sequence);
372 "JBD2: ignoring %d transaction%s from the journal.\n",
373 dropped, (dropped == 1) ? "" : "s");
375 journal->j_transaction_sequence = ++info.end_transaction;
382 static inline unsigned long long read_tag_block(journal_t *journal,
383 journal_block_tag_t *tag)
385 unsigned long long block = be32_to_cpu(tag->t_blocknr);
386 if (jbd2_has_feature_64bit(journal))
387 block |= (u64)be32_to_cpu(tag->t_blocknr_high) << 32;
392 * calc_chksums calculates the checksums for the blocks described in the
395 static int calc_chksums(journal_t *journal, struct buffer_head *bh,
396 unsigned long *next_log_block, __u32 *crc32_sum)
398 int i, num_blks, err;
399 unsigned long io_block;
400 struct buffer_head *obh;
402 num_blks = count_tags(journal, bh);
403 /* Calculate checksum of the descriptor block. */
404 *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
406 for (i = 0; i < num_blks; i++) {
407 io_block = (*next_log_block)++;
408 wrap(journal, *next_log_block);
409 err = jread(&obh, journal, io_block);
411 printk(KERN_ERR "JBD2: IO error %d recovering block "
412 "%lu in log\n", err, io_block);
415 *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
423 static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
425 struct commit_header *h;
429 if (!jbd2_journal_has_csum_v2or3(j))
433 provided = h->h_chksum[0];
435 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
436 h->h_chksum[0] = provided;
438 return provided == cpu_to_be32(calculated);
441 static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
442 journal_block_tag3_t *tag3,
443 void *buf, __u32 sequence)
448 if (!jbd2_journal_has_csum_v2or3(j))
451 seq = cpu_to_be32(sequence);
452 csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
453 csum32 = jbd2_chksum(j, csum32, buf, j->j_blocksize);
455 if (jbd2_has_feature_csum3(j))
456 return tag3->t_checksum == cpu_to_be32(csum32);
458 return tag->t_checksum == cpu_to_be16(csum32);
461 static int do_one_pass(journal_t *journal,
462 struct recovery_info *info, enum passtype pass)
464 unsigned int first_commit_ID, next_commit_ID;
465 unsigned long next_log_block;
466 int err, success = 0;
467 journal_superblock_t * sb;
468 journal_header_t * tmp;
469 struct buffer_head * bh;
470 unsigned int sequence;
472 int tag_bytes = journal_tag_bytes(journal);
473 __u32 crc32_sum = ~0; /* Transactional Checksums */
474 int descr_csum_size = 0;
476 bool need_check_commit_time = false;
477 __u64 last_trans_commit_time = 0, commit_time;
480 * First thing is to establish what we expect to find in the log
481 * (in terms of transaction IDs), and where (in terms of log
482 * block offsets): query the superblock.
485 sb = journal->j_superblock;
486 next_commit_ID = be32_to_cpu(sb->s_sequence);
487 next_log_block = be32_to_cpu(sb->s_start);
489 first_commit_ID = next_commit_ID;
490 if (pass == PASS_SCAN)
491 info->start_transaction = first_commit_ID;
493 jbd2_debug(1, "Starting recovery pass %d\n", pass);
496 * Now we walk through the log, transaction by transaction,
497 * making sure that each transaction has a commit block in the
498 * expected place. Each complete transaction gets replayed back
499 * into the main filesystem.
505 journal_block_tag_t tag;
506 struct buffer_head * obh;
507 struct buffer_head * nbh;
511 /* If we already know where to stop the log traversal,
512 * check right now that we haven't gone past the end of
515 if (pass != PASS_SCAN)
516 if (tid_geq(next_commit_ID, info->end_transaction))
519 jbd2_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
520 next_commit_ID, next_log_block,
521 jbd2_has_feature_fast_commit(journal) ?
522 journal->j_fc_last : journal->j_last);
524 /* Skip over each chunk of the transaction looking
525 * either the next descriptor block or the final commit
528 jbd2_debug(3, "JBD2: checking block %ld\n", next_log_block);
529 err = jread(&bh, journal, next_log_block);
534 wrap(journal, next_log_block);
536 /* What kind of buffer is it?
538 * If it is a descriptor block, check that it has the
539 * expected sequence number. Otherwise, we're all done
542 tmp = (journal_header_t *)bh->b_data;
544 if (tmp->h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER)) {
549 blocktype = be32_to_cpu(tmp->h_blocktype);
550 sequence = be32_to_cpu(tmp->h_sequence);
551 jbd2_debug(3, "Found magic %d, sequence %d\n",
552 blocktype, sequence);
554 if (sequence != next_commit_ID) {
559 /* OK, we have a valid descriptor block which matches
560 * all of the sequence number checks. What are we going
561 * to do with it? That depends on the pass... */
564 case JBD2_DESCRIPTOR_BLOCK:
565 /* Verify checksum first */
566 if (jbd2_journal_has_csum_v2or3(journal))
568 sizeof(struct jbd2_journal_block_tail);
569 if (descr_csum_size > 0 &&
570 !jbd2_descriptor_block_csum_verify(journal,
573 * PASS_SCAN can see stale blocks due to lazy
574 * journal init. Don't error out on those yet.
576 if (pass != PASS_SCAN) {
577 pr_err("JBD2: Invalid checksum recovering block %lu in log\n",
583 need_check_commit_time = true;
585 "invalid descriptor block found in %lu\n",
589 /* If it is a valid descriptor block, replay it
590 * in pass REPLAY; if journal_checksums enabled, then
591 * calculate checksums in PASS_SCAN, otherwise,
592 * just skip over the blocks it describes. */
593 if (pass != PASS_REPLAY) {
594 if (pass == PASS_SCAN &&
595 jbd2_has_feature_checksum(journal) &&
596 !need_check_commit_time &&
597 !info->end_transaction) {
598 if (calc_chksums(journal, bh,
607 next_log_block += count_tags(journal, bh);
608 wrap(journal, next_log_block);
613 /* A descriptor block: we can now write all of
614 * the data blocks. Yay, useful work is finally
615 * getting done here! */
617 tagp = &bh->b_data[sizeof(journal_header_t)];
618 while ((tagp - bh->b_data + tag_bytes)
619 <= journal->j_blocksize - descr_csum_size) {
620 unsigned long io_block;
622 memcpy(&tag, tagp, sizeof(tag));
623 flags = be16_to_cpu(tag.t_flags);
625 io_block = next_log_block++;
626 wrap(journal, next_log_block);
627 err = jread(&obh, journal, io_block);
629 /* Recover what we can, but
630 * report failure at the end. */
633 "JBD2: IO error %d recovering "
634 "block %ld in log\n",
637 unsigned long long blocknr;
639 J_ASSERT(obh != NULL);
640 blocknr = read_tag_block(journal,
643 /* If the block has been
644 * revoked, then we're all done
646 if (jbd2_journal_test_revoke
650 ++info->nr_revoke_hits;
654 /* Look for block corruption */
655 if (!jbd2_block_tag_csum_verify(
656 journal, &tag, (journal_block_tag3_t *)tagp,
657 obh->b_data, be32_to_cpu(tmp->h_sequence))) {
659 success = -EFSBADCRC;
660 printk(KERN_ERR "JBD2: Invalid "
661 "checksum recovering "
662 "data block %llu in "
668 /* Find a buffer for the new
669 * data being restored */
670 nbh = __getblk(journal->j_fs_dev,
672 journal->j_blocksize);
675 "JBD2: Out of memory "
676 "during recovery.\n");
684 memcpy(nbh->b_data, obh->b_data,
685 journal->j_blocksize);
686 if (flags & JBD2_FLAG_ESCAPE) {
687 *((__be32 *)nbh->b_data) =
688 cpu_to_be32(JBD2_MAGIC_NUMBER);
691 BUFFER_TRACE(nbh, "marking dirty");
692 set_buffer_uptodate(nbh);
693 mark_buffer_dirty(nbh);
694 BUFFER_TRACE(nbh, "marking uptodate");
703 if (!(flags & JBD2_FLAG_SAME_UUID))
706 if (flags & JBD2_FLAG_LAST_TAG)
713 case JBD2_COMMIT_BLOCK:
714 /* How to differentiate between interrupted commit
715 * and journal corruption ?
718 * Checksum Verification Failed
720 * ____________________
722 * async_commit sync_commit
724 * | GO TO NEXT "Journal Corruption"
727 * {(n+1)th transanction}
729 * _______|______________
731 * Commit block found Commit block not found
733 * "Journal Corruption" |
734 * _____________|_________
736 * nth trans corrupt OR nth trans
737 * and (n+1)th interrupted interrupted
738 * before commit block
739 * could reach the disk.
740 * (Cannot find the difference in above
741 * mentioned conditions. Hence assume
742 * "Interrupted Commit".)
744 commit_time = be64_to_cpu(
745 ((struct commit_header *)bh->b_data)->h_commit_sec);
747 * If need_check_commit_time is set, it means we are in
748 * PASS_SCAN and csum verify failed before. If
749 * commit_time is increasing, it's the same journal,
750 * otherwise it is stale journal block, just end this
753 if (need_check_commit_time) {
754 if (commit_time >= last_trans_commit_time) {
755 pr_err("JBD2: Invalid checksum found in transaction %u\n",
763 * It likely does not belong to same journal,
764 * just end this recovery with success.
766 jbd2_debug(1, "JBD2: Invalid checksum ignored in transaction %u, likely stale data\n",
773 * Found an expected commit block: if checksums
774 * are present, verify them in PASS_SCAN; else not
775 * much to do other than move on to the next sequence
778 if (pass == PASS_SCAN &&
779 jbd2_has_feature_checksum(journal)) {
780 struct commit_header *cbh =
781 (struct commit_header *)bh->b_data;
782 unsigned found_chksum =
783 be32_to_cpu(cbh->h_chksum[0]);
785 if (info->end_transaction) {
786 journal->j_failed_commit =
787 info->end_transaction;
792 /* Neither checksum match nor unused? */
793 if (!((crc32_sum == found_chksum &&
794 cbh->h_chksum_type ==
796 cbh->h_chksum_size ==
797 JBD2_CRC32_CHKSUM_SIZE) ||
798 (cbh->h_chksum_type == 0 &&
799 cbh->h_chksum_size == 0 &&
805 if (pass == PASS_SCAN &&
806 !jbd2_commit_block_csum_verify(journal,
809 if (commit_time < last_trans_commit_time)
810 goto ignore_crc_mismatch;
811 info->end_transaction = next_commit_ID;
813 if (!jbd2_has_feature_async_commit(journal)) {
814 journal->j_failed_commit =
820 if (pass == PASS_SCAN)
821 last_trans_commit_time = commit_time;
826 case JBD2_REVOKE_BLOCK:
828 * Check revoke block crc in pass_scan, if csum verify
829 * failed, check commit block time later.
831 if (pass == PASS_SCAN &&
832 !jbd2_descriptor_block_csum_verify(journal,
834 jbd2_debug(1, "JBD2: invalid revoke block found in %lu\n",
836 need_check_commit_time = true;
838 /* If we aren't in the REVOKE pass, then we can
839 * just skip over this block. */
840 if (pass != PASS_REVOKE) {
845 err = scan_revoke_records(journal, bh,
846 next_commit_ID, info);
853 jbd2_debug(3, "Unrecognised magic %d, end of scan.\n",
862 * We broke out of the log scan loop: either we came to the
863 * known end of the log or we found an unexpected block in the
864 * log. If the latter happened, then we know that the "current"
865 * transaction marks the end of the valid log.
868 if (pass == PASS_SCAN) {
869 if (!info->end_transaction)
870 info->end_transaction = next_commit_ID;
872 /* It's really bad news if different passes end up at
873 * different places (but possible due to IO errors). */
874 if (info->end_transaction != next_commit_ID) {
875 printk(KERN_ERR "JBD2: recovery pass %d ended at "
876 "transaction %u, expected %u\n",
877 pass, next_commit_ID, info->end_transaction);
883 if (jbd2_has_feature_fast_commit(journal) && pass != PASS_REVOKE) {
884 err = fc_do_one_pass(journal, info, pass);
889 if (block_error && success == 0)
897 /* Scan a revoke record, marking all blocks mentioned as revoked. */
899 static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
900 tid_t sequence, struct recovery_info *info)
902 jbd2_journal_revoke_header_t *header;
904 unsigned csum_size = 0;
908 header = (jbd2_journal_revoke_header_t *) bh->b_data;
909 offset = sizeof(jbd2_journal_revoke_header_t);
910 rcount = be32_to_cpu(header->r_count);
912 if (jbd2_journal_has_csum_v2or3(journal))
913 csum_size = sizeof(struct jbd2_journal_block_tail);
914 if (rcount > journal->j_blocksize - csum_size)
918 if (jbd2_has_feature_64bit(journal))
921 while (offset + record_len <= max) {
922 unsigned long long blocknr;
926 blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
928 blocknr = be64_to_cpu(* ((__be64 *) (bh->b_data+offset)));
929 offset += record_len;
930 err = jbd2_journal_set_revoke(journal, blocknr, sequence);