2 * journal.c --- code for handling the "ext3" journal
4 * Copyright (C) 2000 Andreas Dilger
5 * Copyright (C) 2000 Theodore Ts'o
7 * Parts of the code are based on fs/jfs/journal.c by Stephen C. Tweedie
8 * Copyright (C) 1999 Red Hat Software
10 * This file may be redistributed under the terms of the
11 * GNU General Public License version 2 or at your discretion
16 #ifdef HAVE_SYS_MOUNT_H
17 #include <sys/param.h>
18 #include <sys/mount.h>
19 #define MNT_FL (MS_MGC_VAL | MS_RDONLY)
21 #ifdef HAVE_SYS_STAT_H
25 #define E2FSCK_INCLUDE_INLINE_FUNCS
28 #include "uuid/uuid.h"
30 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
31 static int bh_count = 0;
35 * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths.
36 * This creates a larger static binary, and a smaller binary using
37 * shared libraries. It's also probably slightly less CPU-efficient,
38 * which is why it's not on by default. But, it's a good way of
39 * testing the functions in inode_io.c and fileio.c.
43 /* Checksumming functions */
44 static int e2fsck_journal_verify_csum_type(journal_t *j,
45 journal_superblock_t *jsb)
47 if (!jbd2_journal_has_csum_v2or3(j))
50 return jsb->s_checksum_type == JBD2_CRC32C_CHKSUM;
53 static __u32 e2fsck_journal_sb_csum(journal_superblock_t *jsb)
57 old_crc = jsb->s_checksum;
59 crc = ext2fs_crc32c_le(~0, (unsigned char *)jsb,
60 sizeof(journal_superblock_t));
61 jsb->s_checksum = old_crc;
66 static int e2fsck_journal_sb_csum_verify(journal_t *j,
67 journal_superblock_t *jsb)
69 __u32 provided, calculated;
71 if (!jbd2_journal_has_csum_v2or3(j))
74 provided = ext2fs_be32_to_cpu(jsb->s_checksum);
75 calculated = e2fsck_journal_sb_csum(jsb);
77 return provided == calculated;
80 static errcode_t e2fsck_journal_sb_csum_set(journal_t *j,
81 journal_superblock_t *jsb)
85 if (!jbd2_journal_has_csum_v2or3(j))
88 crc = e2fsck_journal_sb_csum(jsb);
89 jsb->s_checksum = ext2fs_cpu_to_be32(crc);
93 /* Kernel compatibility functions for handling the journal. These allow us
94 * to use the recovery.c file virtually unchanged from the kernel, so we
95 * don't have to do much to keep kernel and user recovery in sync.
97 int jbd2_journal_bmap(journal_t *journal, unsigned long block,
98 unsigned long long *phys)
104 struct inode *inode = journal->j_inode;
113 retval= ext2fs_bmap2(inode->i_ctx->fs, inode->i_ino,
114 &inode->i_ext2, NULL, 0, (blk64_t) block,
117 return -1 * ((int) retval);
121 struct buffer_head *getblk(kdev_t kdev, unsigned long long blocknr,
124 struct buffer_head *bh;
125 int bufsize = sizeof(*bh) + kdev->k_ctx->fs->blocksize -
128 bh = e2fsck_allocate_memory(kdev->k_ctx, bufsize, "block buffer");
132 #ifdef CONFIG_JBD_DEBUG
133 if (journal_enable_debug >= 3)
136 jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n",
137 blocknr, blocksize, bh_count);
139 bh->b_ctx = kdev->k_ctx;
140 if (kdev->k_dev == K_DEV_FS)
141 bh->b_io = kdev->k_ctx->fs->io;
143 bh->b_io = kdev->k_ctx->journal_io;
144 bh->b_size = blocksize;
145 bh->b_blocknr = blocknr;
150 int sync_blockdev(kdev_t kdev)
154 if (kdev->k_dev == K_DEV_FS)
155 io = kdev->k_ctx->fs->io;
157 io = kdev->k_ctx->journal_io;
159 return io_channel_flush(io) ? -EIO : 0;
162 void ll_rw_block(int rw, int op_flags, int nr, struct buffer_head *bhp[])
165 struct buffer_head *bh;
167 for (; nr > 0; --nr) {
169 if (rw == REQ_OP_READ && !bh->b_uptodate) {
170 jfs_debug(3, "reading block %llu/%p\n",
171 bh->b_blocknr, (void *) bh);
172 retval = io_channel_read_blk64(bh->b_io,
176 com_err(bh->b_ctx->device_name, retval,
177 "while reading block %llu\n",
179 bh->b_err = (int) retval;
183 } else if (rw == REQ_OP_WRITE && bh->b_dirty) {
184 jfs_debug(3, "writing block %llu/%p\n",
187 retval = io_channel_write_blk64(bh->b_io,
191 com_err(bh->b_ctx->device_name, retval,
192 "while writing block %llu\n",
194 bh->b_err = (int) retval;
200 jfs_debug(3, "no-op %s for block %llu\n",
201 rw == REQ_OP_READ ? "read" : "write",
207 void mark_buffer_dirty(struct buffer_head *bh)
212 static void mark_buffer_clean(struct buffer_head * bh)
217 void brelse(struct buffer_head *bh)
220 ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
221 jfs_debug(3, "freeing block %llu/%p (total %d)\n",
222 bh->b_blocknr, (void *) bh, --bh_count);
223 ext2fs_free_mem(&bh);
226 int buffer_uptodate(struct buffer_head *bh)
228 return bh->b_uptodate;
231 void mark_buffer_uptodate(struct buffer_head *bh, int val)
233 bh->b_uptodate = val;
236 void wait_on_buffer(struct buffer_head *bh)
239 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
243 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
245 ext2fs_clear_feature_journal_needs_recovery(ctx->fs->super);
247 /* if we had an error doing journal recovery, we need a full fsck */
249 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
250 ext2fs_mark_super_dirty(ctx->fs);
254 * This is a helper function to check the validity of the journal.
256 struct process_block_struct {
257 e2_blkcnt_t last_block;
260 static int process_journal_block(ext2_filsys fs,
262 e2_blkcnt_t blockcnt,
263 blk64_t ref_block EXT2FS_ATTR((unused)),
264 int ref_offset EXT2FS_ATTR((unused)),
267 struct process_block_struct *p;
268 blk64_t blk = *block_nr;
270 p = (struct process_block_struct *) priv_data;
272 if (!blk || blk < fs->super->s_first_data_block ||
273 blk >= ext2fs_blocks_count(fs->super))
277 p->last_block = blockcnt;
281 static int ext4_fc_replay_scan(journal_t *j, struct buffer_head *bh,
282 int off, tid_t expected_tid)
284 e2fsck_t ctx = j->j_fs_dev->k_ctx;
285 struct e2fsck_fc_replay_state *state;
286 int ret = JBD2_FC_REPLAY_CONTINUE;
287 struct ext4_fc_add_range *ext;
288 struct ext4_fc_tl *tl;
289 struct ext4_fc_tail *tail;
291 struct ext4_fc_head *head;
292 struct ext2fs_extent ext2fs_ex;
294 state = &ctx->fc_replay_state;
296 start = (__u8 *)bh->b_data;
297 end = (__u8 *)bh->b_data + j->j_blocksize - 1;
299 jbd_debug(1, "Scan phase starting, expected %d", expected_tid);
300 if (state->fc_replay_expected_off == 0) {
301 memset(state, 0, sizeof(*state));
302 /* Check if we can stop early */
303 if (le16_to_cpu(((struct ext4_fc_tl *)start)->fc_tag)
304 != EXT4_FC_TAG_HEAD) {
305 jbd_debug(1, "Ending early!, not a head tag");
310 if (off != state->fc_replay_expected_off) {
315 state->fc_replay_expected_off++;
316 fc_for_each_tl(start, end, tl) {
317 jbd_debug(3, "Scan phase, tag:%s, blk %lld\n",
318 tag2str(le16_to_cpu(tl->fc_tag)), bh->b_blocknr);
319 switch (le16_to_cpu(tl->fc_tag)) {
320 case EXT4_FC_TAG_ADD_RANGE:
321 ext = (struct ext4_fc_add_range *)ext4_fc_tag_val(tl);
322 ret = ext2fs_decode_extent(&ext2fs_ex, (void *)&ext->fc_ex,
325 ret = JBD2_FC_REPLAY_STOP;
327 ret = JBD2_FC_REPLAY_CONTINUE;
328 case EXT4_FC_TAG_DEL_RANGE:
329 case EXT4_FC_TAG_LINK:
330 case EXT4_FC_TAG_UNLINK:
331 case EXT4_FC_TAG_CREAT:
332 case EXT4_FC_TAG_INODE:
333 case EXT4_FC_TAG_PAD:
335 state->fc_crc = jbd2_chksum(j, state->fc_crc, tl,
336 sizeof(*tl) + ext4_fc_tag_len(tl));
338 case EXT4_FC_TAG_TAIL:
340 tail = (struct ext4_fc_tail *)ext4_fc_tag_val(tl);
341 state->fc_crc = jbd2_chksum(j, state->fc_crc, tl,
343 offsetof(struct ext4_fc_tail,
345 jbd_debug(1, "tail tid %d, expected %d\n",
346 le32_to_cpu(tail->fc_tid),
348 if (le32_to_cpu(tail->fc_tid) == expected_tid &&
349 le32_to_cpu(tail->fc_crc) == state->fc_crc) {
350 state->fc_replay_num_tags = state->fc_cur_tag;
352 ret = state->fc_replay_num_tags ?
353 JBD2_FC_REPLAY_STOP : -EFSBADCRC;
357 case EXT4_FC_TAG_HEAD:
358 head = (struct ext4_fc_head *)ext4_fc_tag_val(tl);
359 if (le32_to_cpu(head->fc_features) &
360 ~EXT4_FC_SUPPORTED_FEATURES) {
364 if (le32_to_cpu(head->fc_tid) != expected_tid) {
369 state->fc_crc = jbd2_chksum(j, state->fc_crc, tl,
370 sizeof(*tl) + ext4_fc_tag_len(tl));
373 ret = state->fc_replay_num_tags ?
374 JBD2_FC_REPLAY_STOP : -ECANCELED;
376 if (ret < 0 || ret == JBD2_FC_REPLAY_STOP)
384 * Main recovery path entry point. This function returns JBD2_FC_REPLAY_CONTINUE
385 * to indicate that it is expecting more fast commit blocks. It returns
386 * JBD2_FC_REPLAY_STOP to indicate that replay is done.
388 static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh,
389 enum passtype pass, int off, tid_t expected_tid)
391 e2fsck_t ctx = journal->j_fs_dev->k_ctx;
392 struct e2fsck_fc_replay_state *state = &ctx->fc_replay_state;
394 if (pass == PASS_SCAN) {
395 state->fc_current_pass = PASS_SCAN;
396 return ext4_fc_replay_scan(journal, bh, off, expected_tid);
398 return JBD2_FC_REPLAY_STOP;
401 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
403 struct process_block_struct pb;
404 struct ext2_super_block *sb = ctx->fs->super;
405 struct ext2_super_block jsuper;
406 struct problem_context pctx;
407 struct buffer_head *bh;
408 struct inode *j_inode = NULL;
409 struct kdev_s *dev_fs = NULL, *dev_journal;
410 const char *journal_name = 0;
411 journal_t *journal = NULL;
412 errcode_t retval = 0;
413 io_manager io_ptr = 0;
414 unsigned long long start = 0;
417 int tried_backup_jnl = 0;
419 clear_problem_context(&pctx);
421 journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
423 return EXT2_ET_NO_MEMORY;
426 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
428 retval = EXT2_ET_NO_MEMORY;
431 dev_journal = dev_fs+1;
433 dev_fs->k_ctx = dev_journal->k_ctx = ctx;
434 dev_fs->k_dev = K_DEV_FS;
435 dev_journal->k_dev = K_DEV_JOURNAL;
437 journal->j_dev = dev_journal;
438 journal->j_fs_dev = dev_fs;
439 journal->j_inode = NULL;
440 journal->j_blocksize = ctx->fs->blocksize;
442 if (uuid_is_null(sb->s_journal_uuid)) {
443 if (!sb->s_journal_inum) {
444 retval = EXT2_ET_BAD_INODE_NUM;
447 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
450 retval = EXT2_ET_NO_MEMORY;
454 j_inode->i_ctx = ctx;
455 j_inode->i_ino = sb->s_journal_inum;
457 if ((retval = ext2fs_read_inode(ctx->fs,
459 &j_inode->i_ext2))) {
461 if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
464 memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
465 memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
467 j_inode->i_ext2.i_size_high = sb->s_jnl_blocks[15];
468 j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
469 j_inode->i_ext2.i_links_count = 1;
470 j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
471 e2fsck_use_inode_shortcuts(ctx, 1);
472 ctx->stashed_ino = j_inode->i_ino;
473 ctx->stashed_inode = &j_inode->i_ext2;
476 if (!j_inode->i_ext2.i_links_count ||
477 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
478 retval = EXT2_ET_NO_JOURNAL;
479 goto try_backup_journal;
481 if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize <
482 JBD2_MIN_JOURNAL_BLOCKS) {
483 retval = EXT2_ET_JOURNAL_TOO_SMALL;
484 goto try_backup_journal;
487 retval = ext2fs_block_iterate3(ctx->fs, j_inode->i_ino,
489 process_journal_block, &pb);
490 if ((pb.last_block + 1) * ctx->fs->blocksize <
491 (int) EXT2_I_SIZE(&j_inode->i_ext2)) {
492 retval = EXT2_ET_JOURNAL_TOO_SMALL;
493 goto try_backup_journal;
495 if (tried_backup_jnl && !(ctx->options & E2F_OPT_READONLY)) {
496 retval = ext2fs_write_inode(ctx->fs, sb->s_journal_inum,
502 journal->j_total_len = EXT2_I_SIZE(&j_inode->i_ext2) /
503 journal->j_blocksize;
506 retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
512 io_ptr = inode_io_manager;
514 journal->j_inode = j_inode;
515 ctx->journal_io = ctx->fs->io;
516 if ((ret = jbd2_journal_bmap(journal, 0, &start)) != 0) {
517 retval = (errcode_t) (-1 * ret);
523 if (!ctx->journal_name) {
526 uuid_unparse(sb->s_journal_uuid, uuid);
527 ctx->journal_name = blkid_get_devname(ctx->blkid,
529 if (!ctx->journal_name)
530 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
532 journal_name = ctx->journal_name;
535 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
536 retval = EXT2_ET_LOAD_EXT_JOURNAL;
540 jfs_debug(1, "Using journal file %s\n", journal_name);
541 io_ptr = unix_io_manager;
545 test_io_backing_manager = io_ptr;
546 io_ptr = test_io_manager;
552 int flags = IO_FLAG_RW;
553 if (!(ctx->mount_flags & EXT2_MF_ISROOT &&
554 ctx->mount_flags & EXT2_MF_READONLY))
555 flags |= IO_FLAG_EXCLUSIVE;
556 if ((ctx->mount_flags & EXT2_MF_READONLY) &&
557 (ctx->options & E2F_OPT_FORCE))
558 flags &= ~IO_FLAG_EXCLUSIVE;
561 retval = io_ptr->open(journal_name, flags,
567 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
572 start = ext2fs_journal_sb_start(ctx->fs->blocksize) - 1;
573 bh = getblk(dev_journal, start, ctx->fs->blocksize);
575 retval = EXT2_ET_NO_MEMORY;
578 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
579 if ((retval = bh->b_err) != 0) {
583 memcpy(&jsuper, start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET,
585 #ifdef WORDS_BIGENDIAN
586 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
587 ext2fs_swap_super(&jsuper);
589 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
590 !ext2fs_has_feature_journal_dev(&jsuper)) {
591 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
592 retval = EXT2_ET_LOAD_EXT_JOURNAL;
596 /* Make sure the journal UUID is correct */
597 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
598 sizeof(jsuper.s_uuid))) {
599 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
600 retval = EXT2_ET_LOAD_EXT_JOURNAL;
605 /* Check the superblock checksum */
606 if (ext2fs_has_feature_metadata_csum(&jsuper)) {
607 struct struct_ext2_filsys fsx;
608 struct ext2_super_block superx;
611 p = start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET;
612 memcpy(&fsx, ctx->fs, sizeof(fsx));
613 memcpy(&superx, ctx->fs->super, sizeof(superx));
615 ext2fs_set_feature_metadata_csum(fsx.super);
616 if (!ext2fs_superblock_csum_verify(&fsx, p) &&
617 fix_problem(ctx, PR_0_EXT_JOURNAL_SUPER_CSUM_INVALID,
619 ext2fs_superblock_csum_set(&fsx, p);
620 mark_buffer_dirty(bh);
625 maxlen = ext2fs_blocks_count(&jsuper);
626 journal->j_total_len = (maxlen < 1ULL << 32) ? maxlen : (1ULL << 32) - 1;
630 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
631 retval = EXT2_ET_NO_MEMORY;
635 journal->j_sb_buffer = bh;
636 journal->j_superblock = (journal_superblock_t *)bh->b_data;
637 if (ext2fs_has_feature_fast_commit(ctx->fs->super))
638 journal->j_fc_replay_callback = ext4_fc_replay;
640 journal->j_fc_replay_callback = NULL;
644 ext2fs_free_mem(&j_inode);
647 *ret_journal = journal;
648 e2fsck_use_inode_shortcuts(ctx, 0);
652 e2fsck_use_inode_shortcuts(ctx, 0);
654 ext2fs_free_mem(&dev_fs);
656 ext2fs_free_mem(&j_inode);
658 ext2fs_free_mem(&journal);
662 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
663 struct problem_context *pctx)
665 struct ext2_super_block *sb = ctx->fs->super;
666 int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super);
667 int has_journal = ext2fs_has_feature_journal(ctx->fs->super);
669 if (has_journal || sb->s_journal_inum) {
670 /* The journal inode is bogus, remove and force full fsck */
671 pctx->ino = sb->s_journal_inum;
672 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
673 if (has_journal && sb->s_journal_inum)
674 printf("*** journal has been deleted ***\n\n");
675 ext2fs_clear_feature_journal(sb);
676 sb->s_journal_inum = 0;
677 memset(sb->s_jnl_blocks, 0, sizeof(sb->s_jnl_blocks));
678 ctx->flags |= E2F_FLAG_JOURNAL_INODE;
679 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
680 e2fsck_clear_recover(ctx, 1);
683 return EXT2_ET_CORRUPT_JOURNAL_SB;
684 } else if (recover) {
685 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
686 e2fsck_clear_recover(ctx, 1);
689 return EXT2_ET_UNSUPP_FEATURE;
694 #define V1_SB_SIZE 0x0024
695 static void clear_v2_journal_fields(journal_t *journal)
697 e2fsck_t ctx = journal->j_dev->k_ctx;
698 struct problem_context pctx;
700 clear_problem_context(&pctx);
702 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
705 ctx->flags |= E2F_FLAG_PROBLEMS_FIXED;
706 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
707 ctx->fs->blocksize-V1_SB_SIZE);
708 mark_buffer_dirty(journal->j_sb_buffer);
712 static errcode_t e2fsck_journal_load(journal_t *journal)
714 e2fsck_t ctx = journal->j_dev->k_ctx;
715 journal_superblock_t *jsb;
716 struct buffer_head *jbh = journal->j_sb_buffer;
717 struct problem_context pctx;
719 clear_problem_context(&pctx);
721 ll_rw_block(REQ_OP_READ, 0, 1, &jbh);
723 com_err(ctx->device_name, jbh->b_err, "%s",
724 _("reading journal superblock\n"));
728 jsb = journal->j_superblock;
729 /* If we don't even have JBD2_MAGIC, we probably have a wrong inode */
730 if (jsb->s_header.h_magic != htonl(JBD2_MAGIC_NUMBER))
731 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
733 switch (ntohl(jsb->s_header.h_blocktype)) {
734 case JBD2_SUPERBLOCK_V1:
735 journal->j_format_version = 1;
736 if (jsb->s_feature_compat ||
737 jsb->s_feature_incompat ||
738 jsb->s_feature_ro_compat ||
740 clear_v2_journal_fields(journal);
743 case JBD2_SUPERBLOCK_V2:
744 journal->j_format_version = 2;
745 if (ntohl(jsb->s_nr_users) > 1 &&
746 uuid_is_null(ctx->fs->super->s_journal_uuid))
747 clear_v2_journal_fields(journal);
748 if (ntohl(jsb->s_nr_users) > 1) {
749 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
750 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
755 * These should never appear in a journal super block, so if
756 * they do, the journal is badly corrupted.
758 case JBD2_DESCRIPTOR_BLOCK:
759 case JBD2_COMMIT_BLOCK:
760 case JBD2_REVOKE_BLOCK:
761 return EXT2_ET_CORRUPT_JOURNAL_SB;
763 /* If we don't understand the superblock major type, but there
764 * is a magic number, then it is likely to be a new format we
765 * just don't understand, so leave it alone. */
767 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
770 if (JBD2_HAS_INCOMPAT_FEATURE(journal, ~JBD2_KNOWN_INCOMPAT_FEATURES))
771 return EXT2_ET_UNSUPP_FEATURE;
773 if (JBD2_HAS_RO_COMPAT_FEATURE(journal, ~JBD2_KNOWN_ROCOMPAT_FEATURES))
774 return EXT2_ET_RO_UNSUPP_FEATURE;
776 /* Checksum v1-3 are mutually exclusive features. */
777 if (jbd2_has_feature_csum2(journal) && jbd2_has_feature_csum3(journal))
778 return EXT2_ET_CORRUPT_JOURNAL_SB;
780 if (jbd2_journal_has_csum_v2or3(journal) &&
781 jbd2_has_feature_checksum(journal))
782 return EXT2_ET_CORRUPT_JOURNAL_SB;
784 if (!e2fsck_journal_verify_csum_type(journal, jsb) ||
785 !e2fsck_journal_sb_csum_verify(journal, jsb))
786 return EXT2_ET_CORRUPT_JOURNAL_SB;
788 if (jbd2_journal_has_csum_v2or3(journal))
789 journal->j_csum_seed = jbd2_chksum(journal, ~0, jsb->s_uuid,
790 sizeof(jsb->s_uuid));
792 /* We have now checked whether we know enough about the journal
793 * format to be able to proceed safely, so any other checks that
794 * fail we should attempt to recover from. */
795 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
796 com_err(ctx->program_name, EXT2_ET_CORRUPT_JOURNAL_SB,
797 _("%s: no valid journal superblock found\n"),
799 return EXT2_ET_CORRUPT_JOURNAL_SB;
802 if (ntohl(jsb->s_maxlen) < journal->j_total_len)
803 journal->j_total_len = ntohl(jsb->s_maxlen);
804 else if (ntohl(jsb->s_maxlen) > journal->j_total_len) {
805 com_err(ctx->program_name, EXT2_ET_CORRUPT_JOURNAL_SB,
806 _("%s: journal too short\n"),
808 return EXT2_ET_CORRUPT_JOURNAL_SB;
811 journal->j_tail_sequence = ntohl(jsb->s_sequence);
812 journal->j_transaction_sequence = journal->j_tail_sequence;
813 journal->j_tail = ntohl(jsb->s_start);
814 journal->j_first = ntohl(jsb->s_first);
815 if (jbd2_has_feature_fast_commit(journal)) {
816 if (ntohl(jsb->s_maxlen) - jbd2_journal_get_num_fc_blks(jsb)
817 < JBD2_MIN_JOURNAL_BLOCKS) {
818 com_err(ctx->program_name, EXT2_ET_CORRUPT_JOURNAL_SB,
819 _("%s: incorrect fast commit blocks\n"),
821 return EXT2_ET_CORRUPT_JOURNAL_SB;
823 journal->j_fc_last = ntohl(jsb->s_maxlen);
824 journal->j_last = journal->j_fc_last -
825 jbd2_journal_get_num_fc_blks(jsb);
826 journal->j_fc_first = journal->j_last + 1;
828 journal->j_last = ntohl(jsb->s_maxlen);
834 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
845 /* Leave a valid existing V1 superblock signature alone.
846 * Anything unrecognisable we overwrite with a new V2
849 if (jsb->s_header.h_magic != htonl(JBD2_MAGIC_NUMBER) ||
850 jsb->s_header.h_blocktype != htonl(JBD2_SUPERBLOCK_V1)) {
851 jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER);
852 jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2);
855 /* Zero out everything else beyond the superblock header */
857 p = ((char *) jsb) + sizeof(journal_header_t);
858 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
860 jsb->s_blocksize = htonl(ctx->fs->blocksize);
861 jsb->s_maxlen = htonl(journal->j_total_len);
862 jsb->s_first = htonl(1);
864 /* Initialize the journal sequence number so that there is "no"
865 * chance we will find old "valid" transactions in the journal.
866 * This avoids the need to zero the whole journal (slow to do,
867 * and risky when we are just recovering the filesystem).
869 uuid_generate(u.uuid);
870 for (i = 0; i < 4; i ++)
872 jsb->s_sequence = htonl(new_seq);
873 e2fsck_journal_sb_csum_set(journal, jsb);
875 mark_buffer_dirty(journal->j_sb_buffer);
876 ll_rw_block(REQ_OP_WRITE, 0, 1, &journal->j_sb_buffer);
879 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
881 struct problem_context *pctx)
883 struct ext2_super_block *sb = ctx->fs->super;
884 int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super);
886 if (ext2fs_has_feature_journal(sb)) {
887 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
888 e2fsck_journal_reset_super(ctx, journal->j_superblock,
890 journal->j_transaction_sequence = 1;
891 e2fsck_clear_recover(ctx, recover);
894 return EXT2_ET_CORRUPT_JOURNAL_SB;
895 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
896 return EXT2_ET_CORRUPT_JOURNAL_SB;
901 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
904 journal_superblock_t *jsb;
907 mark_buffer_clean(journal->j_sb_buffer);
908 else if (!(ctx->options & E2F_OPT_READONLY)) {
909 jsb = journal->j_superblock;
910 jsb->s_sequence = htonl(journal->j_tail_sequence);
912 jsb->s_start = 0; /* this marks the journal as empty */
913 e2fsck_journal_sb_csum_set(journal, jsb);
914 mark_buffer_dirty(journal->j_sb_buffer);
916 brelse(journal->j_sb_buffer);
918 if (ctx->journal_io) {
919 if (ctx->fs && ctx->fs->io != ctx->journal_io)
920 io_channel_close(ctx->journal_io);
925 if (journal->j_inode)
926 ext2fs_free_mem(&journal->j_inode);
928 if (journal->j_fs_dev)
929 ext2fs_free_mem(&journal->j_fs_dev);
930 ext2fs_free_mem(&journal);
934 * This function makes sure that the superblock fields regarding the
935 * journal are consistent.
937 errcode_t e2fsck_check_ext3_journal(e2fsck_t ctx)
939 struct ext2_super_block *sb = ctx->fs->super;
941 int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super);
942 struct problem_context pctx;
944 int reset = 0, force_fsck = 0;
947 /* If we don't have any journal features, don't do anything more */
948 if (!ext2fs_has_feature_journal(sb) &&
949 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
950 uuid_is_null(sb->s_journal_uuid))
953 clear_problem_context(&pctx);
954 pctx.num = sb->s_journal_inum;
956 retval = e2fsck_get_journal(ctx, &journal);
958 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
959 (retval == EXT2_ET_BAD_BLOCK_NUM) ||
960 (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
961 (retval == EXT2_ET_NO_JOURNAL))
962 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
966 retval = e2fsck_journal_load(journal);
968 if ((retval == EXT2_ET_CORRUPT_JOURNAL_SB) ||
969 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
970 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
972 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
973 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
975 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
976 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
977 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
979 e2fsck_journal_release(ctx, journal, 0, 1);
984 * We want to make the flags consistent here. We will not leave with
985 * needs_recovery set but has_journal clear. We can't get in a loop
986 * with -y, -n, or -p, only if a user isn't making up their mind.
989 if (!ext2fs_has_feature_journal(sb)) {
990 recover = ext2fs_has_feature_journal_needs_recovery(sb);
991 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
993 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
996 * Need a full fsck if we are releasing a
997 * journal stored on a reserved inode.
999 force_fsck = recover ||
1000 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
1001 /* Clear all of the journal fields */
1002 sb->s_journal_inum = 0;
1003 sb->s_journal_dev = 0;
1004 memset(sb->s_journal_uuid, 0,
1005 sizeof(sb->s_journal_uuid));
1006 e2fsck_clear_recover(ctx, force_fsck);
1007 } else if (!(ctx->options & E2F_OPT_READONLY)) {
1008 ext2fs_set_feature_journal(sb);
1009 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1010 ext2fs_mark_super_dirty(ctx->fs);
1014 if (ext2fs_has_feature_journal(sb) &&
1015 !ext2fs_has_feature_journal_needs_recovery(sb) &&
1016 journal->j_superblock->s_start != 0) {
1017 /* Print status information */
1018 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
1019 if (ctx->superblock)
1020 problem = PR_0_JOURNAL_RUN_DEFAULT;
1022 problem = PR_0_JOURNAL_RUN;
1023 if (fix_problem(ctx, problem, &pctx)) {
1024 ctx->options |= E2F_OPT_FORCE;
1025 ext2fs_set_feature_journal_needs_recovery(sb);
1026 ext2fs_mark_super_dirty(ctx->fs);
1027 } else if (fix_problem(ctx,
1028 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
1030 sb->s_state &= ~EXT2_VALID_FS;
1031 ext2fs_mark_super_dirty(ctx->fs);
1034 * If the user answers no to the above question, we
1035 * ignore the fact that journal apparently has data;
1036 * accidentally replaying over valid data would be far
1037 * worse than skipping a questionable recovery.
1039 * XXX should we abort with a fatal error here? What
1040 * will the ext3 kernel code do if a filesystem with
1041 * !NEEDS_RECOVERY but with a non-zero
1042 * journal->j_superblock->s_start is mounted?
1047 * If we don't need to do replay the journal, check to see if
1048 * the journal's errno is set; if so, we need to mark the file
1049 * system as being corrupt and clear the journal's s_errno.
1051 if (!ext2fs_has_feature_journal_needs_recovery(sb) &&
1052 journal->j_superblock->s_errno) {
1053 ctx->fs->super->s_state |= EXT2_ERROR_FS;
1054 ext2fs_mark_super_dirty(ctx->fs);
1055 journal->j_superblock->s_errno = 0;
1056 e2fsck_journal_sb_csum_set(journal, journal->j_superblock);
1057 mark_buffer_dirty(journal->j_sb_buffer);
1060 e2fsck_journal_release(ctx, journal, reset, 0);
1064 static errcode_t recover_ext3_journal(e2fsck_t ctx)
1066 struct problem_context pctx;
1070 clear_problem_context(&pctx);
1072 retval = jbd2_journal_init_revoke_record_cache();
1076 retval = jbd2_journal_init_revoke_table_cache();
1080 retval = e2fsck_get_journal(ctx, &journal);
1084 retval = e2fsck_journal_load(journal);
1088 retval = jbd2_journal_init_revoke(journal, 1024);
1092 retval = -jbd2_journal_recover(journal);
1096 if (journal->j_failed_commit) {
1097 pctx.ino = journal->j_failed_commit;
1098 fix_problem(ctx, PR_0_JNL_TXN_CORRUPT, &pctx);
1099 journal->j_superblock->s_errno = -EINVAL;
1100 mark_buffer_dirty(journal->j_sb_buffer);
1103 journal->j_tail_sequence = journal->j_transaction_sequence;
1106 jbd2_journal_destroy_revoke(journal);
1107 jbd2_journal_destroy_revoke_record_cache();
1108 jbd2_journal_destroy_revoke_table_cache();
1109 e2fsck_journal_release(ctx, journal, 1, 0);
1113 errcode_t e2fsck_run_ext3_journal(e2fsck_t ctx)
1115 io_manager io_ptr = ctx->fs->io->manager;
1116 int blocksize = ctx->fs->blocksize;
1117 errcode_t retval, recover_retval;
1119 unsigned long long kbytes_written = 0;
1121 printf(_("%s: recovering journal\n"), ctx->device_name);
1122 if (ctx->options & E2F_OPT_READONLY) {
1123 printf(_("%s: won't do journal recovery while read-only\n"),
1125 return EXT2_ET_FILE_RO;
1128 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
1129 ext2fs_flush(ctx->fs); /* Force out any modifications */
1131 recover_retval = recover_ext3_journal(ctx);
1134 * Reload the filesystem context to get up-to-date data from disk
1135 * because journal recovery will change the filesystem under us.
1137 if (ctx->fs->super->s_kbytes_written &&
1138 ctx->fs->io->manager->get_stats)
1139 ctx->fs->io->manager->get_stats(ctx->fs->io, &stats);
1140 if (stats && stats->bytes_written)
1141 kbytes_written = stats->bytes_written >> 10;
1143 ext2fs_mmp_stop(ctx->fs);
1144 ext2fs_free(ctx->fs);
1145 retval = ext2fs_open(ctx->filesystem_name, ctx->openfs_flags,
1146 ctx->superblock, blocksize, io_ptr,
1149 com_err(ctx->program_name, retval,
1150 _("while trying to re-open %s"),
1152 fatal_error(ctx, 0);
1154 ctx->fs->priv_data = ctx;
1155 ctx->fs->now = ctx->now;
1156 ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1157 ctx->fs->super->s_kbytes_written += kbytes_written;
1159 /* Set the superblock flags */
1160 e2fsck_clear_recover(ctx, recover_retval != 0);
1163 * Do one last sanity check, and propagate journal->s_errno to
1164 * the EXT2_ERROR_FS flag in the fs superblock if needed.
1166 retval = e2fsck_check_ext3_journal(ctx);
1167 return retval ? retval : recover_retval;
1171 * This function will move the journal inode from a visible file in
1172 * the filesystem directory hierarchy to the reserved inode if necessary.
1174 static const char * const journal_names[] = {
1175 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
1177 void e2fsck_move_ext3_journal(e2fsck_t ctx)
1179 struct ext2_super_block *sb = ctx->fs->super;
1180 struct problem_context pctx;
1181 struct ext2_inode inode;
1182 ext2_filsys fs = ctx->fs;
1185 const char * const * cpp;
1189 clear_problem_context(&pctx);
1192 * If the filesystem is opened read-only, or there is no
1193 * journal, then do nothing.
1195 if ((ctx->options & E2F_OPT_READONLY) ||
1196 (sb->s_journal_inum == 0) ||
1197 !ext2fs_has_feature_journal(sb))
1201 * Read in the journal inode
1203 if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
1207 * If it's necessary to backup the journal inode, do so.
1209 if ((sb->s_jnl_backup_type == 0) ||
1210 ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
1211 memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
1212 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
1213 memcpy(sb->s_jnl_blocks, inode.i_block,
1215 sb->s_jnl_blocks[15] = inode.i_size_high;
1216 sb->s_jnl_blocks[16] = inode.i_size;
1217 sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
1218 ext2fs_mark_super_dirty(fs);
1219 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1224 * If the journal is already the hidden inode, then do nothing
1226 if (sb->s_journal_inum == EXT2_JOURNAL_INO)
1230 * The journal inode had better have only one link and not be readable.
1232 if (inode.i_links_count != 1)
1236 * If the filesystem is mounted, or we can't tell whether
1237 * or not it's mounted, do nothing.
1239 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
1240 if (retval || (mount_flags & EXT2_MF_MOUNTED))
1244 * If we can't find the name of the journal inode, then do
1247 for (cpp = journal_names; *cpp; cpp++) {
1248 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
1249 strlen(*cpp), 0, &ino);
1250 if ((retval == 0) && (ino == sb->s_journal_inum))
1256 /* We need the inode bitmap to be loaded */
1257 retval = ext2fs_read_bitmaps(fs);
1262 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
1266 * OK, we've done all the checks, let's actually move the
1267 * journal inode. Errors at this point mean we need to force
1268 * an ext2 filesystem check.
1270 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
1272 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
1274 sb->s_journal_inum = EXT2_JOURNAL_INO;
1275 ext2fs_mark_super_dirty(fs);
1276 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1277 inode.i_links_count = 0;
1278 inode.i_dtime = ctx->now;
1279 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
1282 group = ext2fs_group_of_ino(fs, ino);
1283 ext2fs_unmark_inode_bitmap2(fs->inode_map, ino);
1284 ext2fs_mark_ib_dirty(fs);
1285 ext2fs_bg_free_inodes_count_set(fs, group, ext2fs_bg_free_inodes_count(fs, group) + 1);
1286 ext2fs_group_desc_csum_set(fs, group);
1287 fs->super->s_free_inodes_count++;
1291 pctx.errcode = retval;
1292 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
1293 fs->super->s_state &= ~EXT2_VALID_FS;
1294 ext2fs_mark_super_dirty(fs);
1299 * This function makes sure the superblock hint for the external
1300 * journal is correct.
1302 int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
1304 struct ext2_super_block *sb = ctx->fs->super;
1305 struct problem_context pctx;
1306 char uuid[37], *journal_name;
1309 if (!ext2fs_has_feature_journal(sb) ||
1310 uuid_is_null(sb->s_journal_uuid))
1313 uuid_unparse(sb->s_journal_uuid, uuid);
1314 journal_name = blkid_get_devname(ctx->blkid, "UUID", uuid);
1318 if (stat(journal_name, &st) < 0) {
1323 if (st.st_rdev != sb->s_journal_dev) {
1324 clear_problem_context(&pctx);
1325 pctx.num = st.st_rdev;
1326 if (fix_problem(ctx, PR_0_EXTERNAL_JOURNAL_HINT, &pctx)) {
1327 sb->s_journal_dev = st.st_rdev;
1328 ext2fs_mark_super_dirty(ctx->fs);