2 * linux/fs/jbd2/recovery.c
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
6 * Copyright 1999-2000 Red Hat Software --- All Rights Reserved
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
12 * Journal recovery routines for the generic filesystem journaling code;
13 * part of the ext2fs journaling system.
19 #include <linux/time.h>
21 #include <linux/jbd2.h>
22 #include <linux/errno.h>
23 #include <linux/crc32.h>
24 #include <linux/blkdev.h>
28 * Maintain information about the progress of the recovery job, so that
29 * the different passes can carry information between them.
33 tid_t start_transaction;
34 tid_t end_transaction;
41 enum passtype {PASS_SCAN, PASS_REVOKE, PASS_REPLAY};
42 static int do_one_pass(journal_t *journal,
43 struct recovery_info *info, enum passtype pass);
44 static int scan_revoke_records(journal_t *, struct buffer_head *,
45 tid_t, struct recovery_info *);
49 /* Release readahead buffers after use */
50 static void journal_brelse_array(struct buffer_head *b[], int n)
58 * When reading from the journal, we are going through the block device
59 * layer directly and so there is no readahead being done for us. We
60 * need to implement any readahead ourselves if we want it to happen at
61 * all. Recovery is basically one long sequential read, so make sure we
62 * do the IO in reasonably large chunks.
64 * This is not so critical that we need to be enormously clever about
65 * the readahead size, though. 128K is a purely arbitrary, good-enough
70 static int do_readahead(journal_t *journal, unsigned int start)
73 unsigned int max, nbufs, next;
74 unsigned long long blocknr;
75 struct buffer_head *bh;
77 struct buffer_head * bufs[MAXBUF];
79 /* Do up to 128K of readahead */
80 max = start + (128 * 1024 / journal->j_blocksize);
81 if (max > journal->j_maxlen)
82 max = journal->j_maxlen;
84 /* Do the readahead itself. We'll submit MAXBUF buffer_heads at
85 * a time to the block device IO layer. */
89 for (next = start; next < max; next++) {
90 err = journal_bmap(journal, next, &blocknr);
93 printk(KERN_ERR "JBD2: bad block at offset %u\n",
98 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
104 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
106 if (nbufs == MAXBUF) {
107 ll_rw_block(READ, nbufs, bufs);
108 journal_brelse_array(bufs, nbufs);
116 ll_rw_block(READ, nbufs, bufs);
121 journal_brelse_array(bufs, nbufs);
125 #endif /* __KERNEL__ */
127 static inline __u32 get_be32(__be32 *p)
129 unsigned char *cp = (unsigned char *) p;
133 ret = (ret << 8) + *cp++;
134 ret = (ret << 8) + *cp++;
135 ret = (ret << 8) + *cp++;
139 static inline __u16 get_be16(__be16 *p)
141 unsigned char *cp = (unsigned char *) p;
145 ret = (ret << 8) + *cp++;
150 * Read a block from the journal
153 static int jread(struct buffer_head **bhp, journal_t *journal,
157 unsigned long long blocknr;
158 struct buffer_head *bh;
162 if (offset >= journal->j_maxlen) {
163 printk(KERN_ERR "JBD2: corrupted journal superblock\n");
164 return -EFSCORRUPTED;
167 err = journal_bmap(journal, offset, &blocknr);
170 printk(KERN_ERR "JBD2: bad block at offset %u\n",
175 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
179 if (!buffer_uptodate(bh)) {
180 /* If this is a brand new buffer, start readahead.
181 Otherwise, we assume we are already reading it. */
183 do_readahead(journal, offset);
187 if (!buffer_uptodate(bh)) {
188 printk(KERN_ERR "JBD2: Failed to read block at offset %u\n",
198 static int jbd2_descr_block_csum_verify(journal_t *j,
201 struct journal_block_tail *tail;
205 if (!journal_has_csum_v2or3(j))
208 tail = (struct journal_block_tail *)((char *)buf + j->j_blocksize -
209 sizeof(struct journal_block_tail));
210 provided = tail->t_checksum;
211 tail->t_checksum = 0;
212 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
213 tail->t_checksum = provided;
215 return provided == ext2fs_cpu_to_be32(calculated);
219 * Count the number of in-use tags in a journal descriptor block.
222 static int count_tags(journal_t *journal, struct buffer_head *bh)
225 journal_block_tag_t * tag;
226 int nr = 0, size = journal->j_blocksize;
227 int tag_bytes = journal_tag_bytes(journal);
229 if (journal_has_csum_v2or3(journal))
230 size -= sizeof(struct journal_block_tail);
232 tagp = &bh->b_data[sizeof(journal_header_t)];
234 while ((tagp - bh->b_data + tag_bytes) <= size) {
235 tag = (journal_block_tag_t *) tagp;
239 if (!(get_be16(&tag->t_flags) & JFS_FLAG_SAME_UUID))
242 if (get_be16(&tag->t_flags) & JFS_FLAG_LAST_TAG)
250 /* Make sure we wrap around the log correctly! */
251 #define wrap(journal, var) \
253 if (var >= (journal)->j_last) \
254 var -= ((journal)->j_last - (journal)->j_first); \
258 * journal_recover - recovers a on-disk journal
259 * @journal: the journal to recover
261 * The primary function for recovering the log contents when mounting a
264 * Recovery is done in three passes. In the first pass, we look for the
265 * end of the log. In the second, we assemble the list of revoke
266 * blocks. In the third and final pass, we replay any un-revoked blocks
269 int journal_recover(journal_t *journal)
272 journal_superblock_t * sb;
274 struct recovery_info info;
276 memset(&info, 0, sizeof(info));
277 sb = journal->j_superblock;
280 * The journal superblock's s_start field (the current log head)
281 * is always zero if, and only if, the journal was cleanly
286 jbd_debug(1, "No recovery required, last transaction %d\n",
287 ext2fs_be32_to_cpu(sb->s_sequence));
288 journal->j_transaction_sequence = ext2fs_be32_to_cpu(sb->s_sequence) + 1;
292 err = do_one_pass(journal, &info, PASS_SCAN);
294 err = do_one_pass(journal, &info, PASS_REVOKE);
296 err = do_one_pass(journal, &info, PASS_REPLAY);
298 jbd_debug(1, "JBD2: recovery, exit status %d, "
299 "recovered transactions %u to %u\n",
300 err, info.start_transaction, info.end_transaction);
301 jbd_debug(1, "JBD2: Replayed %d and revoked %d/%d blocks\n",
302 info.nr_replays, info.nr_revoke_hits, info.nr_revokes);
304 /* Restart the log at the next transaction ID, thus invalidating
305 * any existing commit records in the log. */
306 journal->j_transaction_sequence = ++info.end_transaction;
308 journal_clear_revoke(journal);
309 err2 = sync_blockdev(journal->j_fs_dev);
312 /* Make sure all replayed data is on permanent storage */
313 if (journal->j_flags & JFS_BARRIER) {
314 err2 = blkdev_issue_flush(journal->j_fs_dev, GFP_KERNEL, NULL);
322 * journal_skip_recovery - Start journal and wipe exiting records
323 * @journal: journal to startup
325 * Locate any valid recovery information from the journal and set up the
326 * journal structures in memory to ignore it (presumably because the
327 * caller has evidence that it is out of date).
328 * This function does'nt appear to be exorted..
330 * We perform one pass over the journal to allow us to tell the user how
331 * much recovery information is being erased, and to let us initialise
332 * the journal transaction sequence numbers to the next unused ID.
334 int journal_skip_recovery(journal_t *journal)
338 struct recovery_info info;
340 memset (&info, 0, sizeof(info));
342 err = do_one_pass(journal, &info, PASS_SCAN);
345 printk(KERN_ERR "JBD2: error %d scanning journal\n", err);
346 ++journal->j_transaction_sequence;
348 #ifdef CONFIG_JFS_DEBUG
349 int dropped = info.end_transaction -
350 ext2fs_be32_to_cpu(journal->j_superblock->s_sequence);
352 "JBD2: ignoring %d transaction%s from the journal.\n",
353 dropped, (dropped == 1) ? "" : "s");
355 journal->j_transaction_sequence = ++info.end_transaction;
362 static inline unsigned long long read_tag_block(journal_t *journal,
363 journal_block_tag_t *tag)
365 unsigned long long block = get_be32(&tag->t_blocknr);
366 if (jfs_has_feature_64bit(journal))
367 block |= (u64)get_be32(&tag->t_blocknr_high) << 32;
372 * calc_chksums calculates the checksums for the blocks described in the
375 static int calc_chksums(journal_t *journal, struct buffer_head *bh,
376 unsigned long *next_log_block, __u32 *crc32_sum)
378 int i, num_blks, err;
379 unsigned long io_block;
380 struct buffer_head *obh;
382 num_blks = count_tags(journal, bh);
383 /* Calculate checksum of the descriptor block. */
384 *crc32_sum = crc32_be(*crc32_sum, (void *)bh->b_data, bh->b_size);
386 for (i = 0; i < num_blks; i++) {
387 io_block = (*next_log_block)++;
388 wrap(journal, *next_log_block);
389 err = jread(&obh, journal, io_block);
391 printk(KERN_ERR "JBD2: IO error %d recovering block "
392 "%lu in log\n", err, io_block);
395 *crc32_sum = crc32_be(*crc32_sum, (void *)obh->b_data,
403 static int jbd2_commit_block_csum_verify(journal_t *j, void *buf)
405 struct commit_header *h;
409 if (!journal_has_csum_v2or3(j))
413 provided = h->h_chksum[0];
415 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
416 h->h_chksum[0] = provided;
418 return provided == ext2fs_cpu_to_be32(calculated);
421 static int jbd2_block_tag_csum_verify(journal_t *j, journal_block_tag_t *tag,
422 void *buf, __u32 sequence)
424 journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
428 if (!journal_has_csum_v2or3(j))
431 seq = ext2fs_cpu_to_be32(sequence);
432 csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
433 csum32 = jbd2_chksum(j, csum32, buf, j->j_blocksize);
435 if (jfs_has_feature_csum3(j))
436 return get_be32(&tag3->t_checksum) == csum32;
438 return get_be16(&tag->t_checksum) == (csum32 & 0xFFFF);
441 static int do_one_pass(journal_t *journal,
442 struct recovery_info *info, enum passtype pass)
444 unsigned int first_commit_ID, next_commit_ID;
445 unsigned long next_log_block;
446 int err, success = 0;
447 journal_superblock_t * sb;
448 journal_header_t * tmp;
449 struct buffer_head * bh;
450 unsigned int sequence;
452 int tag_bytes = journal_tag_bytes(journal);
453 __u32 crc32_sum = ~0; /* Transactional Checksums */
454 int descr_csum_size = 0;
458 * First thing is to establish what we expect to find in the log
459 * (in terms of transaction IDs), and where (in terms of log
460 * block offsets): query the superblock.
463 sb = journal->j_superblock;
464 next_commit_ID = ext2fs_be32_to_cpu(sb->s_sequence);
465 next_log_block = ext2fs_be32_to_cpu(sb->s_start);
467 first_commit_ID = next_commit_ID;
468 if (pass == PASS_SCAN)
469 info->start_transaction = first_commit_ID;
471 jbd_debug(1, "Starting recovery pass %d\n", pass);
474 * Now we walk through the log, transaction by transaction,
475 * making sure that each transaction has a commit block in the
476 * expected place. Each complete transaction gets replayed back
477 * into the main filesystem.
483 journal_block_tag_t * tag;
484 struct buffer_head * obh;
485 struct buffer_head * nbh;
489 /* If we already know where to stop the log traversal,
490 * check right now that we haven't gone past the end of
493 if (pass != PASS_SCAN)
494 if (tid_geq(next_commit_ID, info->end_transaction))
497 jbd_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
498 next_commit_ID, next_log_block, journal->j_last);
500 /* Skip over each chunk of the transaction looking
501 * either the next descriptor block or the final commit
504 jbd_debug(3, "JBD2: checking block %ld\n", next_log_block);
505 err = jread(&bh, journal, next_log_block);
510 wrap(journal, next_log_block);
512 /* What kind of buffer is it?
514 * If it is a descriptor block, check that it has the
515 * expected sequence number. Otherwise, we're all done
518 tmp = (journal_header_t *)bh->b_data;
520 if (tmp->h_magic != ext2fs_cpu_to_be32(JFS_MAGIC_NUMBER)) {
525 blocktype = ext2fs_be32_to_cpu(tmp->h_blocktype);
526 sequence = ext2fs_be32_to_cpu(tmp->h_sequence);
527 jbd_debug(3, "Found magic %d, sequence %d\n",
528 blocktype, sequence);
530 if (sequence != next_commit_ID) {
535 /* OK, we have a valid descriptor block which matches
536 * all of the sequence number checks. What are we going
537 * to do with it? That depends on the pass... */
540 case JFS_DESCRIPTOR_BLOCK:
541 /* Verify checksum first */
542 if (journal_has_csum_v2or3(journal))
544 sizeof(struct journal_block_tail);
545 if (descr_csum_size > 0 &&
546 !jbd2_descr_block_csum_verify(journal,
553 /* If it is a valid descriptor block, replay it
554 * in pass REPLAY; if journal_checksums enabled, then
555 * calculate checksums in PASS_SCAN, otherwise,
556 * just skip over the blocks it describes. */
557 if (pass != PASS_REPLAY) {
558 if (pass == PASS_SCAN &&
559 jfs_has_feature_checksum(journal) &&
560 !info->end_transaction) {
561 if (calc_chksums(journal, bh,
570 next_log_block += count_tags(journal, bh);
571 wrap(journal, next_log_block);
576 /* A descriptor block: we can now write all of
577 * the data blocks. Yay, useful work is finally
578 * getting done here! */
580 tagp = &bh->b_data[sizeof(journal_header_t)];
581 while ((tagp - bh->b_data + tag_bytes)
582 <= journal->j_blocksize - descr_csum_size) {
583 unsigned long io_block;
585 tag = (journal_block_tag_t *) tagp;
586 flags = get_be16(&tag->t_flags);
588 io_block = next_log_block++;
589 wrap(journal, next_log_block);
590 err = jread(&obh, journal, io_block);
592 /* Recover what we can, but
593 * report failure at the end. */
596 "JBD2: IO error %d recovering "
597 "block %ld in log\n",
600 unsigned long long blocknr;
602 J_ASSERT(obh != NULL);
603 blocknr = read_tag_block(journal,
606 /* If the block has been
607 * revoked, then we're all done
609 if (journal_test_revoke
613 ++info->nr_revoke_hits;
617 /* Look for block corruption */
618 if (!jbd2_block_tag_csum_verify(
619 journal, tag, obh->b_data,
620 ext2fs_be32_to_cpu(tmp->h_sequence))) {
622 success = -EFSBADCRC;
623 printk(KERN_ERR "JBD2: Invalid "
624 "checksum recovering "
625 "block %llu in log\n",
631 /* Find a buffer for the new
632 * data being restored */
633 nbh = __getblk(journal->j_fs_dev,
635 journal->j_blocksize);
638 "JBD2: Out of memory "
639 "during recovery.\n");
647 memcpy(nbh->b_data, obh->b_data,
648 journal->j_blocksize);
649 if (flags & JFS_FLAG_ESCAPE) {
650 __u32 magic = ext2fs_cpu_to_be32(JFS_MAGIC_NUMBER);
651 memcpy(nbh->b_data, &magic,
655 BUFFER_TRACE(nbh, "marking dirty");
656 set_buffer_uptodate(nbh);
657 mark_buffer_dirty(nbh);
658 BUFFER_TRACE(nbh, "marking uptodate");
660 /* ll_rw_block(WRITE, 1, &nbh); */
668 if (!(flags & JFS_FLAG_SAME_UUID))
671 if (flags & JFS_FLAG_LAST_TAG)
678 case JFS_COMMIT_BLOCK:
679 /* How to differentiate between interrupted commit
680 * and journal corruption ?
683 * Checksum Verification Failed
685 * ____________________
687 * async_commit sync_commit
689 * | GO TO NEXT "Journal Corruption"
692 * {(n+1)th transanction}
694 * _______|______________
696 * Commit block found Commit block not found
698 * "Journal Corruption" |
699 * _____________|_________
701 * nth trans corrupt OR nth trans
702 * and (n+1)th interrupted interrupted
703 * before commit block
704 * could reach the disk.
705 * (Cannot find the difference in above
706 * mentioned conditions. Hence assume
707 * "Interrupted Commit".)
710 /* Found an expected commit block: if checksums
711 * are present verify them in PASS_SCAN; else not
712 * much to do other than move on to the next sequence
714 if (pass == PASS_SCAN &&
715 jfs_has_feature_checksum(journal)) {
716 int chksum_err, chksum_seen;
717 struct commit_header *cbh =
718 (struct commit_header *)bh->b_data;
719 unsigned found_chksum =
720 ext2fs_be32_to_cpu(cbh->h_chksum[0]);
722 chksum_err = chksum_seen = 0;
724 if (info->end_transaction) {
725 journal->j_failed_commit =
726 info->end_transaction;
731 if (crc32_sum == found_chksum &&
732 cbh->h_chksum_type == JFS_CRC32_CHKSUM &&
733 cbh->h_chksum_size ==
734 JFS_CRC32_CHKSUM_SIZE)
736 else if (!(cbh->h_chksum_type == 0 &&
737 cbh->h_chksum_size == 0 &&
741 * If fs is mounted using an old kernel and then
742 * kernel with journal_chksum is used then we
743 * get a situation where the journal flag has
744 * checksum flag set but checksums are not
745 * present i.e chksum = 0, in the individual
747 * Hence to avoid checksum failures, in this
748 * situation, this extra check is added.
753 info->end_transaction = next_commit_ID;
755 if (!jfs_has_feature_async_commit(journal)){
756 journal->j_failed_commit =
764 if (pass == PASS_SCAN &&
765 !jbd2_commit_block_csum_verify(journal,
767 info->end_transaction = next_commit_ID;
769 if (!jfs_has_feature_async_commit(journal)) {
770 journal->j_failed_commit =
780 case JFS_REVOKE_BLOCK:
781 /* If we aren't in the REVOKE pass, then we can
782 * just skip over this block. */
783 if (pass != PASS_REVOKE) {
788 err = scan_revoke_records(journal, bh,
789 next_commit_ID, info);
796 jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
805 * We broke out of the log scan loop: either we came to the
806 * known end of the log or we found an unexpected block in the
807 * log. If the latter happened, then we know that the "current"
808 * transaction marks the end of the valid log.
811 if (pass == PASS_SCAN) {
812 if (!info->end_transaction)
813 info->end_transaction = next_commit_ID;
815 /* It's really bad news if different passes end up at
816 * different places (but possible due to IO errors). */
817 if (info->end_transaction != next_commit_ID) {
818 printk(KERN_ERR "JBD2: recovery pass %d ended at "
819 "transaction %u, expected %u\n",
820 pass, next_commit_ID, info->end_transaction);
825 if (block_error && success == 0)
833 static int jbd2_revoke_block_csum_verify(journal_t *j,
836 struct journal_revoke_tail *tail;
840 if (!journal_has_csum_v2or3(j))
843 tail = (struct journal_revoke_tail *)((char *)buf + j->j_blocksize -
844 sizeof(struct journal_revoke_tail));
845 provided = tail->r_checksum;
846 tail->r_checksum = 0;
847 calculated = jbd2_chksum(j, j->j_csum_seed, buf, j->j_blocksize);
848 tail->r_checksum = provided;
850 return provided == ext2fs_cpu_to_be32(calculated);
853 /* Scan a revoke record, marking all blocks mentioned as revoked. */
855 static int scan_revoke_records(journal_t *journal, struct buffer_head *bh,
856 tid_t sequence, struct recovery_info *info)
858 journal_revoke_header_t *header;
860 unsigned csum_size = 0;
864 header = (journal_revoke_header_t *) bh->b_data;
865 offset = sizeof(journal_revoke_header_t);
866 rcount = ext2fs_be32_to_cpu(header->r_count);
868 if (!jbd2_revoke_block_csum_verify(journal, header))
871 if (journal_has_csum_v2or3(journal))
872 csum_size = sizeof(struct journal_revoke_tail);
873 if (rcount > journal->j_blocksize - csum_size)
877 if (jfs_has_feature_64bit(journal))
880 while (offset + record_len <= max) {
881 unsigned long long blocknr;
885 blocknr = ext2fs_be32_to_cpu(* ((__u32 *) (bh->b_data+offset)));
887 blocknr = ext2fs_be64_to_cpu(* ((__u64 *) (bh->b_data+offset)));
888 offset += record_len;
889 err = journal_set_revoke(journal, blocknr, sequence);