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 ll_rw_block(REQ_OP_READ, 0, nbufs, bufs);
104 journal_brelse_array(bufs, nbufs);
112 ll_rw_block(REQ_OP_READ, 0, nbufs, bufs);
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)) {
155 /* If this is a brand new buffer, start readahead.
156 Otherwise, we assume we are already reading it. */
158 do_readahead(journal, offset);
162 if (!buffer_uptodate(bh)) {
163 printk(KERN_ERR "JBD2: Failed to read block at offset %u\n",
173 static int jbd2_descriptor_block_csum_verify(journal_t *j, void *buf)
175 struct jbd2_journal_block_tail *tail;
179 if (!jbd2_journal_has_csum_v2or3(j))
182 tail = (struct jbd2_journal_block_tail *)((char *)buf +
183 j->j_blocksize - sizeof(struct jbd2_journal_block_tail));
184 provided = tail->t_checksum;
185 tail->t_checksum = 0;
186 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
187 tail->t_checksum = provided;
189 return provided == cpu_to_be32(calculated);
193 * Count the number of in-use tags in a journal descriptor block.
196 static int count_tags(journal_t *journal, struct buffer_head *bh)
199 journal_block_tag_t tag;
200 int nr = 0, size = journal->j_blocksize;
201 int tag_bytes = journal_tag_bytes(journal);
203 if (jbd2_journal_has_csum_v2or3(journal))
204 size -= sizeof(struct jbd2_journal_block_tail);
206 tagp = &bh->b_data[sizeof(journal_header_t)];
208 while ((tagp - bh->b_data + tag_bytes) <= size) {
209 memcpy(&tag, tagp, sizeof(tag));
213 if (!(tag.t_flags & cpu_to_be16(JBD2_FLAG_SAME_UUID)))
216 if (tag.t_flags & cpu_to_be16(JBD2_FLAG_LAST_TAG))
224 /* Make sure we wrap around the log correctly! */
225 #define wrap(journal, var) \
227 if (var >= (journal)->j_last) \
228 var -= ((journal)->j_last - (journal)->j_first); \
231 static int fc_do_one_pass(journal_t *journal,
232 struct recovery_info *info, enum passtype pass)
234 unsigned int expected_commit_id = info->end_transaction;
235 unsigned long next_fc_block;
236 struct buffer_head *bh;
239 next_fc_block = journal->j_fc_first;
240 if (!journal->j_fc_replay_callback)
243 while (next_fc_block <= journal->j_fc_last) {
244 jbd2_debug(3, "Fast commit replay: next block %ld\n",
246 err = jread(&bh, journal, next_fc_block);
248 jbd2_debug(3, "Fast commit replay: read error\n");
252 err = journal->j_fc_replay_callback(journal, bh, pass,
253 next_fc_block - journal->j_fc_first,
257 if (err < 0 || err == JBD2_FC_REPLAY_STOP)
263 jbd2_debug(3, "Fast commit replay failed, err = %d\n", err);
269 * jbd2_journal_recover - recovers a on-disk journal
270 * @journal: the journal to recover
272 * The primary function for recovering the log contents when mounting a
275 * Recovery is done in three passes. In the first pass, we look for the
276 * end of the log. In the second, we assemble the list of revoke
277 * blocks. In the third and final pass, we replay any un-revoked blocks
280 int jbd2_journal_recover(journal_t *journal)
283 journal_superblock_t * sb;
285 struct recovery_info info;
287 memset(&info, 0, sizeof(info));
288 sb = journal->j_superblock;
291 * The journal superblock's s_start field (the current log head)
292 * is always zero if, and only if, the journal was cleanly
297 jbd2_debug(1, "No recovery required, last transaction %d\n",
298 be32_to_cpu(sb->s_sequence));
299 journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
303 err = do_one_pass(journal, &info, PASS_SCAN);
305 err = do_one_pass(journal, &info, PASS_REVOKE);
307 err = do_one_pass(journal, &info, PASS_REPLAY);
309 jbd2_debug(1, "JBD2: recovery, exit status %d, "
310 "recovered transactions %u to %u\n",
311 err, info.start_transaction, info.end_transaction);
312 jbd2_debug(1, "JBD2: Replayed %d and revoked %d/%d blocks\n",
313 info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
315 /* Restart the log at the next transaction ID, thus invalidating
316 * any existing commit records in the log. */
317 journal->j_transaction_sequence = ++info.end_transaction;
319 jbd2_journal_clear_revoke(journal);
320 err2 = sync_blockdev(journal->j_fs_dev);
323 /* Make sure all replayed data is on permanent storage */
324 if (journal->j_flags & JBD2_BARRIER) {
325 err2 = blkdev_issue_flush(journal->j_fs_dev);
333 * jbd2_journal_skip_recovery - Start journal and wipe exiting records
334 * @journal: journal to startup
336 * Locate any valid recovery information from the journal and set up the
337 * journal structures in memory to ignore it (presumably because the
338 * caller has evidence that it is out of date).
339 * This function doesn't appear to be exported..
341 * We perform one pass over the journal to allow us to tell the user how
342 * much recovery information is being erased, and to let us initialise
343 * the journal transaction sequence numbers to the next unused ID.
345 int jbd2_journal_skip_recovery(journal_t *journal)
349 struct recovery_info info;
351 memset (&info, 0, sizeof(info));
353 err = do_one_pass(journal, &info, PASS_SCAN);
356 printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
357 ++journal->j_transaction_sequence;
359 #ifdef CONFIG_JBD2_DEBUG
360 int dropped = info.end_transaction -
361 be32_to_cpu(journal->j_superblock->s_sequence);
363 "JBD2: ignoring %d transaction%s from the journal.\n",
364 dropped, (dropped == 1) ? "" : "s");
366 journal->j_transaction_sequence = ++info.end_transaction;
373 static inline unsigned long long read_tag_block(journal_t *journal,
374 journal_block_tag_t *tag)
376 unsigned long long block = be32_to_cpu(tag->t_blocknr);
377 if (jbd2_has_feature_64bit(journal))
378 block |= (u64)be32_to_cpu(tag->t_blocknr_high) << 32;
383 * calc_chksums calculates the checksums for the blocks described in the
386 static int calc_chksums(journal_t *journal, struct buffer_head *bh,
387 unsigned long *next_log_block, __u32 *crc32_sum)
389 int i, num_blks, err;
390 unsigned long io_block;
391 struct buffer_head *obh;
393 num_blks = count_tags(journal, bh);
394 /* Calculate checksum of the descriptor block. */
395 *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
397 for (i = 0; i < num_blks; i++) {
398 io_block = (*next_log_block)++;
399 wrap(journal, *next_log_block);
400 err = jread(&obh, journal, io_block);
402 printk(KERN_ERR "JBD2: IO error %d recovering block "
403 "%lu in log\n", err, io_block);
406 *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
414 static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
416 struct commit_header *h;
420 if (!jbd2_journal_has_csum_v2or3(j))
424 provided = h->h_chksum[0];
426 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
427 h->h_chksum[0] = provided;
429 return provided == cpu_to_be32(calculated);
432 static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
433 journal_block_tag3_t *tag3,
434 void *buf, __u32 sequence)
439 if (!jbd2_journal_has_csum_v2or3(j))
442 seq = cpu_to_be32(sequence);
443 csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
444 csum32 = jbd2_chksum(j, csum32, buf, j->j_blocksize);
446 if (jbd2_has_feature_csum3(j))
447 return tag3->t_checksum == cpu_to_be32(csum32);
449 return tag->t_checksum == cpu_to_be16(csum32);
452 static int do_one_pass(journal_t *journal,
453 struct recovery_info *info, enum passtype pass)
455 unsigned int first_commit_ID, next_commit_ID;
456 unsigned long next_log_block;
457 int err, success = 0;
458 journal_superblock_t * sb;
459 journal_header_t * tmp;
460 struct buffer_head * bh;
461 unsigned int sequence;
463 int tag_bytes = journal_tag_bytes(journal);
464 __u32 crc32_sum = ~0; /* Transactional Checksums */
465 int descr_csum_size = 0;
467 bool need_check_commit_time = false;
468 __u64 last_trans_commit_time = 0, commit_time;
471 * First thing is to establish what we expect to find in the log
472 * (in terms of transaction IDs), and where (in terms of log
473 * block offsets): query the superblock.
476 sb = journal->j_superblock;
477 next_commit_ID = be32_to_cpu(sb->s_sequence);
478 next_log_block = be32_to_cpu(sb->s_start);
480 first_commit_ID = next_commit_ID;
481 if (pass == PASS_SCAN)
482 info->start_transaction = first_commit_ID;
484 jbd2_debug(1, "Starting recovery pass %d\n", pass);
487 * Now we walk through the log, transaction by transaction,
488 * making sure that each transaction has a commit block in the
489 * expected place. Each complete transaction gets replayed back
490 * into the main filesystem.
496 journal_block_tag_t tag;
497 struct buffer_head * obh;
498 struct buffer_head * nbh;
502 /* If we already know where to stop the log traversal,
503 * check right now that we haven't gone past the end of
506 if (pass != PASS_SCAN)
507 if (tid_geq(next_commit_ID, info->end_transaction))
510 jbd2_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
511 next_commit_ID, next_log_block, journal->j_last);
513 /* Skip over each chunk of the transaction looking
514 * either the next descriptor block or the final commit
517 jbd2_debug(3, "JBD2: checking block %ld\n", next_log_block);
518 err = jread(&bh, journal, next_log_block);
523 wrap(journal, next_log_block);
525 /* What kind of buffer is it?
527 * If it is a descriptor block, check that it has the
528 * expected sequence number. Otherwise, we're all done
531 tmp = (journal_header_t *)bh->b_data;
533 if (tmp->h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER)) {
538 blocktype = be32_to_cpu(tmp->h_blocktype);
539 sequence = be32_to_cpu(tmp->h_sequence);
540 jbd2_debug(3, "Found magic %d, sequence %d\n",
541 blocktype, sequence);
543 if (sequence != next_commit_ID) {
548 /* OK, we have a valid descriptor block which matches
549 * all of the sequence number checks. What are we going
550 * to do with it? That depends on the pass... */
553 case JBD2_DESCRIPTOR_BLOCK:
554 /* Verify checksum first */
555 if (jbd2_journal_has_csum_v2or3(journal))
557 sizeof(struct jbd2_journal_block_tail);
558 if (descr_csum_size > 0 &&
559 !jbd2_descriptor_block_csum_verify(journal,
562 * PASS_SCAN can see stale blocks due to lazy
563 * journal init. Don't error out on those yet.
565 if (pass != PASS_SCAN) {
566 pr_err("JBD2: Invalid checksum recovering block %lu in log\n",
572 need_check_commit_time = true;
574 "invalid descriptor block found in %lu\n",
578 /* If it is a valid descriptor block, replay it
579 * in pass REPLAY; if journal_checksums enabled, then
580 * calculate checksums in PASS_SCAN, otherwise,
581 * just skip over the blocks it describes. */
582 if (pass != PASS_REPLAY) {
583 if (pass == PASS_SCAN &&
584 jbd2_has_feature_checksum(journal) &&
585 !need_check_commit_time &&
586 !info->end_transaction) {
587 if (calc_chksums(journal, bh,
596 next_log_block += count_tags(journal, bh);
597 wrap(journal, next_log_block);
602 /* A descriptor block: we can now write all of
603 * the data blocks. Yay, useful work is finally
604 * getting done here! */
606 tagp = &bh->b_data[sizeof(journal_header_t)];
607 while ((tagp - bh->b_data + tag_bytes)
608 <= journal->j_blocksize - descr_csum_size) {
609 unsigned long io_block;
611 memcpy(&tag, tagp, sizeof(tag));
612 flags = be16_to_cpu(tag.t_flags);
614 io_block = next_log_block++;
615 wrap(journal, next_log_block);
616 err = jread(&obh, journal, io_block);
618 /* Recover what we can, but
619 * report failure at the end. */
622 "JBD2: IO error %d recovering "
623 "block %ld in log\n",
626 unsigned long long blocknr;
628 J_ASSERT(obh != NULL);
629 blocknr = read_tag_block(journal,
632 /* If the block has been
633 * revoked, then we're all done
635 if (jbd2_journal_test_revoke
639 ++info->nr_revoke_hits;
643 /* Look for block corruption */
644 if (!jbd2_block_tag_csum_verify(
645 journal, &tag, (journal_block_tag3_t *)tagp,
646 obh->b_data, be32_to_cpu(tmp->h_sequence))) {
648 success = -EFSBADCRC;
649 printk(KERN_ERR "JBD2: Invalid "
650 "checksum recovering "
651 "data block %llu in "
657 /* Find a buffer for the new
658 * data being restored */
659 nbh = __getblk(journal->j_fs_dev,
661 journal->j_blocksize);
664 "JBD2: Out of memory "
665 "during recovery.\n");
673 memcpy(nbh->b_data, obh->b_data,
674 journal->j_blocksize);
675 if (flags & JBD2_FLAG_ESCAPE) {
676 *((__be32 *)nbh->b_data) =
677 cpu_to_be32(JBD2_MAGIC_NUMBER);
680 BUFFER_TRACE(nbh, "marking dirty");
681 set_buffer_uptodate(nbh);
682 mark_buffer_dirty(nbh);
683 BUFFER_TRACE(nbh, "marking uptodate");
685 /* ll_rw_block(WRITE, 1, &nbh); */
693 if (!(flags & JBD2_FLAG_SAME_UUID))
696 if (flags & JBD2_FLAG_LAST_TAG)
703 case JBD2_COMMIT_BLOCK:
704 /* How to differentiate between interrupted commit
705 * and journal corruption ?
708 * Checksum Verification Failed
710 * ____________________
712 * async_commit sync_commit
714 * | GO TO NEXT "Journal Corruption"
717 * {(n+1)th transanction}
719 * _______|______________
721 * Commit block found Commit block not found
723 * "Journal Corruption" |
724 * _____________|_________
726 * nth trans corrupt OR nth trans
727 * and (n+1)th interrupted interrupted
728 * before commit block
729 * could reach the disk.
730 * (Cannot find the difference in above
731 * mentioned conditions. Hence assume
732 * "Interrupted Commit".)
734 commit_time = be64_to_cpu(
735 ((struct commit_header *)bh->b_data)->h_commit_sec);
737 * If need_check_commit_time is set, it means we are in
738 * PASS_SCAN and csum verify failed before. If
739 * commit_time is increasing, it's the same journal,
740 * otherwise it is stale journal block, just end this
743 if (need_check_commit_time) {
744 if (commit_time >= last_trans_commit_time) {
745 pr_err("JBD2: Invalid checksum found in transaction %u\n",
753 * It likely does not belong to same journal,
754 * just end this recovery with success.
756 jbd2_debug(1, "JBD2: Invalid checksum ignored in transaction %u, likely stale data\n",
763 * Found an expected commit block: if checksums
764 * are present, verify them in PASS_SCAN; else not
765 * much to do other than move on to the next sequence
768 if (pass == PASS_SCAN &&
769 jbd2_has_feature_checksum(journal)) {
770 struct commit_header *cbh =
771 (struct commit_header *)bh->b_data;
772 unsigned found_chksum =
773 be32_to_cpu(cbh->h_chksum[0]);
775 if (info->end_transaction) {
776 journal->j_failed_commit =
777 info->end_transaction;
782 /* Neither checksum match nor unused? */
783 if (!((crc32_sum == found_chksum &&
784 cbh->h_chksum_type ==
786 cbh->h_chksum_size ==
787 JBD2_CRC32_CHKSUM_SIZE) ||
788 (cbh->h_chksum_type == 0 &&
789 cbh->h_chksum_size == 0 &&
795 if (pass == PASS_SCAN &&
796 !jbd2_commit_block_csum_verify(journal,
799 if (commit_time < last_trans_commit_time)
800 goto ignore_crc_mismatch;
801 info->end_transaction = next_commit_ID;
803 if (!jbd2_has_feature_async_commit(journal)) {
804 journal->j_failed_commit =
810 if (pass == PASS_SCAN)
811 last_trans_commit_time = commit_time;
816 case JBD2_REVOKE_BLOCK:
818 * Check revoke block crc in pass_scan, if csum verify
819 * failed, check commit block time later.
821 if (pass == PASS_SCAN &&
822 !jbd2_descriptor_block_csum_verify(journal,
824 jbd2_debug(1, "JBD2: invalid revoke block found in %lu\n",
826 need_check_commit_time = true;
828 /* If we aren't in the REVOKE pass, then we can
829 * just skip over this block. */
830 if (pass != PASS_REVOKE) {
835 err = scan_revoke_records(journal, bh,
836 next_commit_ID, info);
843 jbd2_debug(3, "Unrecognised magic %d, end of scan.\n",
852 * We broke out of the log scan loop: either we came to the
853 * known end of the log or we found an unexpected block in the
854 * log. If the latter happened, then we know that the "current"
855 * transaction marks the end of the valid log.
858 if (pass == PASS_SCAN) {
859 if (!info->end_transaction)
860 info->end_transaction = next_commit_ID;
862 /* It's really bad news if different passes end up at
863 * different places (but possible due to IO errors). */
864 if (info->end_transaction != next_commit_ID) {
865 printk(KERN_ERR "JBD2: recovery pass %d ended at "
866 "transaction %u, expected %u\n",
867 pass, next_commit_ID, info->end_transaction);
873 if (jbd2_has_feature_fast_commit(journal) && pass != PASS_REVOKE) {
874 err = fc_do_one_pass(journal, info, pass);
879 if (block_error && success == 0)
887 /* Scan a revoke record, marking all blocks mentioned as revoked. */
889 static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
890 tid_t sequence, struct recovery_info *info)
892 jbd2_journal_revoke_header_t *header;
894 unsigned csum_size = 0;
898 header = (jbd2_journal_revoke_header_t *) bh->b_data;
899 offset = sizeof(jbd2_journal_revoke_header_t);
900 rcount = be32_to_cpu(header->r_count);
902 if (jbd2_journal_has_csum_v2or3(journal))
903 csum_size = sizeof(struct jbd2_journal_block_tail);
904 if (rcount > journal->j_blocksize - csum_size)
908 if (jbd2_has_feature_64bit(journal))
911 while (offset + record_len <= max) {
912 unsigned long long blocknr;
916 blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
918 blocknr = be64_to_cpu(* ((__be64 *) (bh->b_data+offset)));
919 offset += record_len;
920 err = jbd2_journal_set_revoke(journal, blocknr, sequence);