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 enum passtype {PASS_SCAN, PASS_REVOKE, PASS_REPLAY};
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_maxlen)
79 max = journal->j_maxlen;
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 ll_rw_block(REQ_OP_READ, 0, nbufs, bufs);
105 journal_brelse_array(bufs, nbufs);
113 ll_rw_block(REQ_OP_READ, 0, nbufs, bufs);
118 journal_brelse_array(bufs, nbufs);
122 #endif /* __KERNEL__ */
124 static inline __u32 get_be32(__be32 *p)
126 unsigned char *cp = (unsigned char *) p;
130 ret = (ret << 8) + *cp++;
131 ret = (ret << 8) + *cp++;
132 ret = (ret << 8) + *cp++;
136 static inline __u16 get_be16(__be16 *p)
138 unsigned char *cp = (unsigned char *) p;
142 ret = (ret << 8) + *cp++;
147 * Read a block from the journal
150 static int jread(struct buffer_head **bhp, journal_t *journal,
154 unsigned long long blocknr;
155 struct buffer_head *bh;
159 if (offset >= journal->j_maxlen) {
160 printk(KERN_ERR "JBD2: corrupted journal superblock\n");
161 return -EFSCORRUPTED;
164 err = jbd2_journal_bmap(journal, offset, &blocknr);
167 printk(KERN_ERR "JBD2: bad block at offset %u\n",
172 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
176 if (!buffer_uptodate(bh)) {
177 /* If this is a brand new buffer, start readahead.
178 Otherwise, we assume we are already reading it. */
180 do_readahead(journal, offset);
184 if (!buffer_uptodate(bh)) {
185 printk(KERN_ERR "JBD2: Failed to read block at offset %u\n",
195 static int jbd2_descriptor_block_csum_verify(journal_t *j, void *buf)
197 struct jbd2_journal_block_tail *tail;
201 if (!jbd2_journal_has_csum_v2or3(j))
204 tail = (struct jbd2_journal_block_tail *)((char *)buf + j->j_blocksize -
205 sizeof(struct jbd2_journal_block_tail));
206 provided = tail->t_checksum;
207 tail->t_checksum = 0;
208 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
209 tail->t_checksum = provided;
211 return provided == cpu_to_be32(calculated);
215 * Count the number of in-use tags in a journal descriptor block.
218 static int count_tags(journal_t *journal, struct buffer_head *bh)
221 journal_block_tag_t * tag;
222 int nr = 0, size = journal->j_blocksize;
223 int tag_bytes = journal_tag_bytes(journal);
225 if (jbd2_journal_has_csum_v2or3(journal))
226 size -= sizeof(struct jbd2_journal_block_tail);
228 tagp = &bh->b_data[sizeof(journal_header_t)];
230 while ((tagp - bh->b_data + tag_bytes) <= size) {
231 tag = (journal_block_tag_t *) tagp;
235 if (!(get_be16(&tag->t_flags) & JBD2_FLAG_SAME_UUID))
238 if (get_be16(&tag->t_flags) & JBD2_FLAG_LAST_TAG)
246 /* Make sure we wrap around the log correctly! */
247 #define wrap(journal, var) \
249 if (var >= (journal)->j_last) \
250 var -= ((journal)->j_last - (journal)->j_first); \
254 * jbd2_journal_recover - recovers a on-disk journal
255 * @journal: the journal to recover
257 * The primary function for recovering the log contents when mounting a
260 * Recovery is done in three passes. In the first pass, we look for the
261 * end of the log. In the second, we assemble the list of revoke
262 * blocks. In the third and final pass, we replay any un-revoked blocks
265 int jbd2_journal_recover(journal_t *journal)
268 journal_superblock_t * sb;
270 struct recovery_info info;
272 memset(&info, 0, sizeof(info));
273 sb = journal->j_superblock;
276 * The journal superblock's s_start field (the current log head)
277 * is always zero if, and only if, the journal was cleanly
282 jbd_debug(1, "No recovery required, last transaction %d\n",
283 be32_to_cpu(sb->s_sequence));
284 journal->j_transaction_sequence = be32_to_cpu(sb->s_sequence) + 1;
288 err = do_one_pass(journal, &info, PASS_SCAN);
290 err = do_one_pass(journal, &info, PASS_REVOKE);
292 err = do_one_pass(journal, &info, PASS_REPLAY);
294 jbd_debug(1, "JBD2: recovery, exit status %d, "
295 "recovered transactions %u to %u\n",
296 err, info.start_transaction, info.end_transaction);
297 jbd_debug(1, "JBD2: Replayed %d and revoked %d/%d blocks\n",
298 info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
300 /* Restart the log at the next transaction ID, thus invalidating
301 * any existing commit records in the log. */
302 journal->j_transaction_sequence = ++info.end_transaction;
304 jbd2_journal_clear_revoke(journal);
305 err2 = sync_blockdev(journal->j_fs_dev);
308 /* Make sure all replayed data is on permanent storage */
309 if (journal->j_flags & JBD2_BARRIER) {
310 err2 = blkdev_issue_flush(journal->j_fs_dev, GFP_KERNEL, NULL);
318 * jbd2_journal_skip_recovery - Start journal and wipe exiting records
319 * @journal: journal to startup
321 * Locate any valid recovery information from the journal and set up the
322 * journal structures in memory to ignore it (presumably because the
323 * caller has evidence that it is out of date).
324 * This function doesn't appear to be exported..
326 * We perform one pass over the journal to allow us to tell the user how
327 * much recovery information is being erased, and to let us initialise
328 * the journal transaction sequence numbers to the next unused ID.
330 int jbd2_journal_skip_recovery(journal_t *journal)
334 struct recovery_info info;
336 memset (&info, 0, sizeof(info));
338 err = do_one_pass(journal, &info, PASS_SCAN);
341 printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
342 ++journal->j_transaction_sequence;
344 #ifdef CONFIG_JBD2_DEBUG
345 int dropped = info.end_transaction -
346 be32_to_cpu(journal->j_superblock->s_sequence);
348 "JBD2: ignoring %d transaction%s from the journal.\n",
349 dropped, (dropped == 1) ? "" : "s");
351 journal->j_transaction_sequence = ++info.end_transaction;
358 static inline unsigned long long read_tag_block(journal_t *journal,
359 journal_block_tag_t *tag)
361 unsigned long long block = get_be32(&tag->t_blocknr);
362 if (jbd2_has_feature_64bit(journal))
363 block |= (u64)get_be32(&tag->t_blocknr_high) << 32;
368 * calc_chksums calculates the checksums for the blocks described in the
371 static int calc_chksums(journal_t *journal, struct buffer_head *bh,
372 unsigned long *next_log_block, __u32 *crc32_sum)
374 int i, num_blks, err;
375 unsigned long io_block;
376 struct buffer_head *obh;
378 num_blks = count_tags(journal, bh);
379 /* Calculate checksum of the descriptor block. */
380 *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
382 for (i = 0; i < num_blks; i++) {
383 io_block = (*next_log_block)++;
384 wrap(journal, *next_log_block);
385 err = jread(&obh, journal, io_block);
387 printk(KERN_ERR "JBD2: IO error %d recovering block "
388 "%lu in log\n", err, io_block);
391 *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
399 static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
401 struct commit_header *h;
405 if (!jbd2_journal_has_csum_v2or3(j))
409 provided = h->h_chksum[0];
411 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
412 h->h_chksum[0] = provided;
414 return provided == cpu_to_be32(calculated);
417 static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
418 void *buf, __u32 sequence)
420 journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
424 if (!jbd2_journal_has_csum_v2or3(j))
427 seq = cpu_to_be32(sequence);
428 csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
429 csum32 = jbd2_chksum(j, csum32, buf, j->j_blocksize);
431 if (jbd2_has_feature_csum3(j))
432 return get_be32(&tag3->t_checksum) == csum32;
434 return get_be16(&tag->t_checksum) == (csum32 & 0xFFFF);
437 static int do_one_pass(journal_t *journal,
438 struct recovery_info *info, enum passtype pass)
440 unsigned int first_commit_ID, next_commit_ID;
441 unsigned long next_log_block;
442 int err, success = 0;
443 journal_superblock_t * sb;
444 journal_header_t * tmp;
445 struct buffer_head * bh;
446 unsigned int sequence;
448 int tag_bytes = journal_tag_bytes(journal);
449 __u32 crc32_sum = ~0; /* Transactional Checksums */
450 int descr_csum_size = 0;
454 * First thing is to establish what we expect to find in the log
455 * (in terms of transaction IDs), and where (in terms of log
456 * block offsets): query the superblock.
459 sb = journal->j_superblock;
460 next_commit_ID = be32_to_cpu(sb->s_sequence);
461 next_log_block = be32_to_cpu(sb->s_start);
463 first_commit_ID = next_commit_ID;
464 if (pass == PASS_SCAN)
465 info->start_transaction = first_commit_ID;
467 jbd_debug(1, "Starting recovery pass %d\n", pass);
470 * Now we walk through the log, transaction by transaction,
471 * making sure that each transaction has a commit block in the
472 * expected place. Each complete transaction gets replayed back
473 * into the main filesystem.
479 journal_block_tag_t * tag;
480 struct buffer_head * obh;
481 struct buffer_head * nbh;
485 /* If we already know where to stop the log traversal,
486 * check right now that we haven't gone past the end of
489 if (pass != PASS_SCAN)
490 if (tid_geq(next_commit_ID, info->end_transaction))
493 jbd_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
494 next_commit_ID, next_log_block, journal->j_last);
496 /* Skip over each chunk of the transaction looking
497 * either the next descriptor block or the final commit
500 jbd_debug(3, "JBD2: checking block %ld\n", next_log_block);
501 err = jread(&bh, journal, next_log_block);
506 wrap(journal, next_log_block);
508 /* What kind of buffer is it?
510 * If it is a descriptor block, check that it has the
511 * expected sequence number. Otherwise, we're all done
514 tmp = (journal_header_t *)bh->b_data;
516 if (tmp->h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER)) {
521 blocktype = be32_to_cpu(tmp->h_blocktype);
522 sequence = be32_to_cpu(tmp->h_sequence);
523 jbd_debug(3, "Found magic %d, sequence %d\n",
524 blocktype, sequence);
526 if (sequence != next_commit_ID) {
531 /* OK, we have a valid descriptor block which matches
532 * all of the sequence number checks. What are we going
533 * to do with it? That depends on the pass... */
536 case JBD2_DESCRIPTOR_BLOCK:
537 /* Verify checksum first */
538 if (jbd2_journal_has_csum_v2or3(journal))
540 sizeof(struct jbd2_journal_block_tail);
541 if (descr_csum_size > 0 &&
542 !jbd2_descriptor_block_csum_verify(journal,
549 /* If it is a valid descriptor block, replay it
550 * in pass REPLAY; if journal_checksums enabled, then
551 * calculate checksums in PASS_SCAN, otherwise,
552 * just skip over the blocks it describes. */
553 if (pass != PASS_REPLAY) {
554 if (pass == PASS_SCAN &&
555 jbd2_has_feature_checksum(journal) &&
556 !info->end_transaction) {
557 if (calc_chksums(journal, bh,
566 next_log_block += count_tags(journal, bh);
567 wrap(journal, next_log_block);
572 /* A descriptor block: we can now write all of
573 * the data blocks. Yay, useful work is finally
574 * getting done here! */
576 tagp = &bh->b_data[sizeof(journal_header_t)];
577 while ((tagp - bh->b_data + tag_bytes)
578 <= journal->j_blocksize - descr_csum_size) {
579 unsigned long io_block;
581 tag = (journal_block_tag_t *) tagp;
582 flags = get_be16(&tag->t_flags);
584 io_block = next_log_block++;
585 wrap(journal, next_log_block);
586 err = jread(&obh, journal, io_block);
588 /* Recover what we can, but
589 * report failure at the end. */
592 "JBD2: IO error %d recovering "
593 "block %ld in log\n",
596 unsigned long long blocknr;
598 J_ASSERT(obh != NULL);
599 blocknr = read_tag_block(journal,
602 /* If the block has been
603 * revoked, then we're all done
605 if (jbd2_journal_test_revoke
609 ++info->nr_revoke_hits;
613 /* Look for block corruption */
614 if (!jbd2_block_tag_csum_verify(
615 journal, tag, obh->b_data,
616 be32_to_cpu(tmp->h_sequence))) {
618 success = -EFSBADCRC;
619 printk(KERN_ERR "JBD2: Invalid "
620 "checksum recovering "
621 "data block %llu in "
627 /* Find a buffer for the new
628 * data being restored */
629 nbh = __getblk(journal->j_fs_dev,
631 journal->j_blocksize);
634 "JBD2: Out of memory "
635 "during recovery.\n");
643 memcpy(nbh->b_data, obh->b_data,
644 journal->j_blocksize);
645 if (flags & JBD2_FLAG_ESCAPE) {
646 __be32 magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
647 memcpy(nbh->b_data, &magic,
651 BUFFER_TRACE(nbh, "marking dirty");
652 set_buffer_uptodate(nbh);
653 mark_buffer_dirty(nbh);
654 BUFFER_TRACE(nbh, "marking uptodate");
656 /* ll_rw_block(WRITE, 1, &nbh); */
664 if (!(flags & JBD2_FLAG_SAME_UUID))
667 if (flags & JBD2_FLAG_LAST_TAG)
674 case JBD2_COMMIT_BLOCK:
675 /* How to differentiate between interrupted commit
676 * and journal corruption ?
679 * Checksum Verification Failed
681 * ____________________
683 * async_commit sync_commit
685 * | GO TO NEXT "Journal Corruption"
688 * {(n+1)th transaction}
690 * _______|______________
692 * Commit block found Commit block not found
694 * "Journal Corruption" |
695 * _____________|_________
697 * nth trans corrupt OR nth trans
698 * and (n+1)th interrupted interrupted
699 * before commit block
700 * could reach the disk.
701 * (Cannot find the difference in above
702 * mentioned conditions. Hence assume
703 * "Interrupted Commit".)
706 /* Found an expected commit block: if checksums
707 * are present verify them in PASS_SCAN; else not
708 * much to do other than move on to the next sequence
710 if (pass == PASS_SCAN &&
711 jbd2_has_feature_checksum(journal)) {
712 int chksum_err, chksum_seen;
713 struct commit_header *cbh =
714 (struct commit_header *)bh->b_data;
715 unsigned found_chksum =
716 be32_to_cpu(cbh->h_chksum[0]);
718 chksum_err = chksum_seen = 0;
720 if (info->end_transaction) {
721 journal->j_failed_commit =
722 info->end_transaction;
727 if (crc32_sum == found_chksum &&
728 cbh->h_chksum_type == JBD2_CRC32_CHKSUM &&
729 cbh->h_chksum_size ==
730 JBD2_CRC32_CHKSUM_SIZE)
732 else if (!(cbh->h_chksum_type == 0 &&
733 cbh->h_chksum_size == 0 &&
737 * If fs is mounted using an old kernel and then
738 * kernel with journal_chksum is used then we
739 * get a situation where the journal flag has
740 * checksum flag set but checksums are not
741 * present i.e chksum = 0, in the individual
743 * Hence to avoid checksum failures, in this
744 * situation, this extra check is added.
749 info->end_transaction = next_commit_ID;
751 if (!jbd2_has_feature_async_commit(journal)) {
752 journal->j_failed_commit =
760 if (pass == PASS_SCAN &&
761 !jbd2_commit_block_csum_verify(journal,
763 info->end_transaction = next_commit_ID;
765 if (!jbd2_has_feature_async_commit(journal)) {
766 journal->j_failed_commit =
776 case JBD2_REVOKE_BLOCK:
777 /* If we aren't in the REVOKE pass, then we can
778 * just skip over this block. */
779 if (pass != PASS_REVOKE) {
784 err = scan_revoke_records(journal, bh,
785 next_commit_ID, info);
792 jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
801 * We broke out of the log scan loop: either we came to the
802 * known end of the log or we found an unexpected block in the
803 * log. If the latter happened, then we know that the "current"
804 * transaction marks the end of the valid log.
807 if (pass == PASS_SCAN) {
808 if (!info->end_transaction)
809 info->end_transaction = next_commit_ID;
811 /* It's really bad news if different passes end up at
812 * different places (but possible due to IO errors). */
813 if (info->end_transaction != next_commit_ID) {
814 printk(KERN_ERR "JBD2: recovery pass %d ended at "
815 "transaction %u, expected %u\n",
816 pass, next_commit_ID, info->end_transaction);
821 if (block_error && success == 0)
829 /* Scan a revoke record, marking all blocks mentioned as revoked. */
831 static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
832 tid_t sequence, struct recovery_info *info)
834 jbd2_journal_revoke_header_t *header;
836 unsigned csum_size = 0;
840 header = (jbd2_journal_revoke_header_t *) bh->b_data;
841 offset = sizeof(jbd2_journal_revoke_header_t);
842 rcount = be32_to_cpu(header->r_count);
844 if (!jbd2_descriptor_block_csum_verify(journal, header))
847 if (jbd2_journal_has_csum_v2or3(journal))
848 csum_size = sizeof(struct jbd2_journal_block_tail);
849 if (rcount > journal->j_blocksize - csum_size)
853 if (jbd2_has_feature_64bit(journal))
856 while (offset + record_len <= max) {
857 unsigned long long blocknr;
861 blocknr = be32_to_cpu(* ((__be32 *) (bh->b_data+offset)));
863 blocknr = be64_to_cpu(* ((__be64 *) (bh->b_data+offset)));
864 offset += record_len;
865 err = jbd2_journal_set_revoke(journal, blocknr, sequence);