Whamcloud - gitweb
Update for 1.24 release.
[tools/e2fsprogs.git] / e2fsck / journal.c
index fd9ed49..fd71921 100644 (file)
 #include <sys/stat.h>
 #endif
 
+#define E2FSCK_INCLUDE_INLINE_FUNCS
 #include "jfs_user.h"
 #include "problem.h"
 #include "uuid/uuid.h"
 
-#ifdef JFS_DEBUG
+#ifdef JFS_DEBUG               /* Enabled by configure --enable-jfs-debug */
 static int bh_count = 0;
-int journal_enable_debug = 0;
+int journal_enable_debug = 2;
 #endif
 
+/* Kernel compatibility functions for handling the journal.  These allow us
+ * to use the recovery.c file virtually unchanged from the kernel, so we
+ * don't have to do much to keep kernel and user recovery in sync.
+ */
 int bmap(struct inode *inode, int block)
 {
        int retval;
@@ -54,7 +59,7 @@ struct buffer_head *getblk(e2fsck_t ctx, blk_t blocknr, int blocksize)
                return NULL;
 
        jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
-                 blocknr, blocksize, ++bh_count);
+                 (unsigned long) blocknr, blocksize, ++bh_count);
 
        bh->b_ctx = ctx;
        bh->b_size = blocksize;
@@ -72,8 +77,8 @@ void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
                bh = *bhp++;
                if (rw == READ && !bh->b_uptodate) {
                        jfs_debug(3, "reading block %lu/%p\n", 
-                                 bh->b_blocknr, bh);
-                       retval = io_channel_read_blk(bh->b_ctx->fs->io, 
+                                 (unsigned long) bh->b_blocknr, (void *) bh);
+                       retval = io_channel_read_blk(bh->b_ctx->journal_io, 
                                                     bh->b_blocknr,
                                                     1, bh->b_data);
                        if (retval) {
@@ -86,8 +91,8 @@ void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
                        bh->b_uptodate = 1;
                } else if (rw == WRITE && bh->b_dirty) {
                        jfs_debug(3, "writing block %lu/%p\n", 
-                                 bh->b_blocknr, bh);
-                       retval = io_channel_write_blk(bh->b_ctx->fs->io, 
+                                 (unsigned long) bh->b_blocknr, (void *) bh);
+                       retval = io_channel_write_blk(bh->b_ctx->journal_io, 
                                                      bh->b_blocknr,
                                                      1, bh->b_data);
                        if (retval) {
@@ -102,7 +107,7 @@ void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
                } else
                        jfs_debug(3, "no-op %s for block %lu\n",
                                  rw == READ ? "read" : "write", 
-                                 bh->b_blocknr);
+                                 (unsigned long) bh->b_blocknr);
        }
 }
 
@@ -111,12 +116,17 @@ void mark_buffer_dirty(struct buffer_head *bh, int dummy)
        bh->b_dirty = dummy | 1; /* use dummy to avoid unused variable */
 }
 
+static void mark_buffer_clean(struct buffer_head * bh)
+{
+       bh->b_dirty = 0;
+}
+
 void brelse(struct buffer_head *bh)
 {
        if (bh->b_dirty)
                ll_rw_block(WRITE, 1, &bh);
        jfs_debug(3, "freeing block %lu/%p (total %d)\n",
-                 bh->b_blocknr, bh, --bh_count);
+                 (unsigned long) bh->b_blocknr, (void *) bh, --bh_count);
        ext2fs_free_mem((void **) &bh);
 }
 
@@ -125,6 +135,11 @@ int buffer_uptodate(struct buffer_head *bh)
        return bh->b_uptodate;
 }
 
+void mark_buffer_uptodate(struct buffer_head *bh, int val)
+{
+       bh->b_uptodate = val;
+}
+
 void wait_on_buffer(struct buffer_head *bh)
 {
        if (!bh->b_uptodate)
@@ -134,26 +149,24 @@ void wait_on_buffer(struct buffer_head *bh)
 
 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
 {
-       struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
-
-       s->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
+       ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
 
        /* if we had an error doing journal recovery, we need a full fsck */
        if (error)
-               s->s_state &= ~EXT2_VALID_FS;
+               ctx->fs->super->s_state &= ~EXT2_VALID_FS;
        ext2fs_mark_super_dirty(ctx->fs);
 }
 
-static int e2fsck_journal_init_inode(e2fsck_t ctx, struct ext2fs_sb *s,
-                                    ino_t journal_inum, journal_t **journal)
+static errcode_t e2fsck_journal_init_inode(e2fsck_t ctx,
+                                          struct ext2_super_block *s,
+                                          journal_t **journal)
 {
        struct inode *inode;
-       const char *cmdname = ctx->program_name;
        struct buffer_head *bh;
        blk_t start;
        int retval;
 
-       jfs_debug(1, "Using journal inode %lu\n", journal_inum);
+       jfs_debug(1, "Using journal inode %u\n", s->s_journal_inum);
        *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
        if (!*journal) {
                return EXT2_ET_NO_MEMORY;
@@ -166,8 +179,8 @@ static int e2fsck_journal_init_inode(e2fsck_t ctx, struct ext2fs_sb *s,
        }
 
        inode->i_ctx = ctx;
-       inode->i_ino = journal_inum;
-       retval = ext2fs_read_inode(ctx->fs, journal_inum, &inode->i_ext2);
+       inode->i_ino = s->s_journal_inum;
+       retval = ext2fs_read_inode(ctx->fs, s->s_journal_inum, &inode->i_ext2);
        if (retval)
                goto exit_inode;
 
@@ -175,6 +188,7 @@ static int e2fsck_journal_init_inode(e2fsck_t ctx, struct ext2fs_sb *s,
        (*journal)->j_inode = inode;
        (*journal)->j_blocksize = ctx->fs->blocksize;
        (*journal)->j_maxlen = inode->i_ext2.i_size / (*journal)->j_blocksize;
+       ctx->journal_io = ctx->fs->io;
 
        if (!inode->i_ext2.i_links_count ||
            !LINUX_S_ISREG(inode->i_ext2.i_mode) ||
@@ -191,7 +205,7 @@ static int e2fsck_journal_init_inode(e2fsck_t ctx, struct ext2fs_sb *s,
        }
        (*journal)->j_sb_buffer = bh;
        (*journal)->j_superblock = (journal_superblock_t *)bh->b_data;
-
+       
        return 0;
 
 exit_inode:
@@ -202,77 +216,127 @@ exit_journal:
        return retval;
 }
 
-static int e2fsck_get_journal(e2fsck_t ctx, journal_t **journal)
+static errcode_t e2fsck_journal_init_dev(e2fsck_t ctx,
+                                        struct ext2_super_block *s,
+                                        journal_t **journal)
 {
-       char uuid_str[40];
+       struct buffer_head *bh;
+       io_manager      io_ptr;
+       blk_t           start;
+       int             retval;
+       int             blocksize = ctx->fs->blocksize;
+       struct ext2_super_block jsuper;
        struct problem_context pctx;
-       struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
-       int recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
+       const char      *journal_name;
 
        clear_problem_context(&pctx);
+       journal_name = ctx->journal_name;
+       if (!journal_name)
+               journal_name = ext2fs_find_block_device(s->s_journal_dev);
 
-       if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
-               if (s->s_journal_dev) {
-                       pctx.num = s->s_journal_dev;
-                       /* this problem aborts on -y, -p, unsupported on -n */
-                       if (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_DEV, &pctx))
-                               return EXT2_ET_UNSUPP_FEATURE;
-                       s->s_journal_dev = 0;
-                       s->s_state &= ~EXT2_VALID_FS;
-                       ext2fs_mark_super_dirty(ctx->fs);
-               }
-               if (!uuid_is_null(s->s_journal_uuid)) {
-                       uuid_unparse(s->s_journal_uuid, uuid_str);
-                       pctx.str = uuid_str;
-                       /* this problem aborts on -y, -p, unsupported on -n */
-                       if (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_UUID, &pctx))
-                               return EXT2_ET_UNSUPP_FEATURE;
-                       uuid_clear(s->s_journal_uuid);
-                       s->s_state &= ~EXT2_VALID_FS;
-                       ext2fs_mark_super_dirty(ctx->fs);
-               }
-               if (!s->s_journal_inum)
-                       return EXT2_ET_BAD_INODE_NUM;
+       if (!journal_name) {
+               fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
+               return EXT2_ET_LOAD_EXT_JOURNAL;
        }
 
-       if (s->s_journal_dev) {
-               pctx.num = s->s_journal_dev;
-               if (!fix_problem(ctx, PR_0_JOURNAL_BAD_DEV, &pctx))
-                       return EXT2_ET_UNSUPP_FEATURE;
-               s->s_journal_dev = 0;
-               s->s_state &= ~EXT2_VALID_FS;
-               ext2fs_mark_super_dirty(ctx->fs);
+       jfs_debug(1, "Using journal file %s\n", journal_name);
+
+#if 1
+       io_ptr = unix_io_manager;
+#else
+       io_ptr = test_io_manager;
+       test_io_backing_manager = unix_io_manager;
+#endif
+       retval = io_ptr->open(journal_name, IO_FLAG_RW, &ctx->journal_io);
+       if (!ctx->journal_name)
+               free((void *) journal_name);
+       if (retval)
+               return retval;
+
+       io_channel_set_blksize(ctx->journal_io, blocksize);
+       start = (blocksize == 1024) ? 1 : 0;
+       bh = getblk(ctx, start, blocksize);
+       if (!bh)
+               return EXT2_ET_NO_MEMORY;
+       ll_rw_block(READ, 1, &bh);
+       if (bh->b_err)
+               return bh->b_err;
+       memcpy(&jsuper, start ? bh->b_data :  bh->b_data + 1024,
+              sizeof(jsuper));
+       brelse(bh);
+#ifdef EXT2FS_ENABLE_SWAPFS
+       if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) 
+               ext2fs_swap_super(&jsuper);
+#endif
+       if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
+           !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
+               fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
+               return EXT2_ET_LOAD_EXT_JOURNAL;
        }
-       if (!uuid_is_null(s->s_journal_uuid)) {
-               uuid_unparse(s->s_journal_uuid, uuid_str);
-               pctx.str = uuid_str;
-               if (!fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx))
-                       return EXT2_ET_UNSUPP_FEATURE;
-               uuid_clear(s->s_journal_uuid);
-               s->s_state &= ~EXT2_VALID_FS;
-               ext2fs_mark_super_dirty(ctx->fs);
+       /* Make sure the journal UUID is correct */
+       if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
+                  sizeof(jsuper.s_uuid))) {
+               fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
+               return EXT2_ET_LOAD_EXT_JOURNAL;
        }
+               
+       *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
+       if (!*journal) {
+               return EXT2_ET_NO_MEMORY;
+       }
+
+       (*journal)->j_dev = ctx;
+       (*journal)->j_inode = NULL;
+       (*journal)->j_blocksize = ctx->fs->blocksize;
+       (*journal)->j_maxlen = jsuper.s_blocks_count;
 
-       return e2fsck_journal_init_inode(ctx, s, s->s_journal_inum, journal);
+       bh = getblk(ctx, start+1, (*journal)->j_blocksize);
+       if (!bh) {
+               retval = EXT2_ET_NO_MEMORY;
+               goto errout;
+       }
+       (*journal)->j_sb_buffer = bh;
+       (*journal)->j_superblock = (journal_superblock_t *)bh->b_data;
+       
+       return 0;
+
+errout:
+       ext2fs_free_mem((void **)journal);
+       return retval;
+}
+
+static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **journal)
+{
+       struct ext2_super_block *sb = ctx->fs->super;
+
+       if (uuid_is_null(sb->s_journal_uuid)) {
+               if (!sb->s_journal_inum)
+                       return EXT2_ET_BAD_INODE_NUM;
+               return e2fsck_journal_init_inode(ctx, sb, journal);
+       } else {
+               return e2fsck_journal_init_dev(ctx, sb, journal);
+       }
 }
 
-static int e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
-                                       struct problem_context *pctx)
+static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
+                                             struct problem_context *pctx)
 {
-       struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
-       int recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
-       int has_journal = s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL;
+       struct ext2_super_block *sb = ctx->fs->super;
+       int recover = ctx->fs->super->s_feature_incompat &
+               EXT3_FEATURE_INCOMPAT_RECOVER;
+       int has_journal = ctx->fs->super->s_feature_compat &
+               EXT3_FEATURE_COMPAT_HAS_JOURNAL;
 
-       if (has_journal || s->s_journal_inum) {
+       if (has_journal || sb->s_journal_inum) {
                /* The journal inode is bogus, remove and force full fsck */
+               pctx->ino = sb->s_journal_inum;
                if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
-                       struct ext2fs_sb *s =(struct ext2fs_sb *)ctx->fs->super;
-
-                       if (has_journal && s->s_journal_inum)
+                       if (has_journal && sb->s_journal_inum)
                                printf("*** ext3 journal has been deleted - "
                                       "filesystem is now ext2 only ***\n\n");
-                       s->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
-                       s->s_journal_inum = 0;
+                       sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
+                       sb->s_journal_inum = 0;
+                       ctx->flags |= E2F_FLAG_JOURNAL_INODE; /* FIXME: todo */
                        e2fsck_clear_recover(ctx, 1);
                        return 0;
                }
@@ -287,28 +351,7 @@ static int e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
        return 0;
 }
 
-static int e2fsck_journal_fix_unsupported_super(e2fsck_t ctx,
-                                               struct problem_context *pctx)
-{
-       struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
-
-       /* Unsupported journal superblock - first choice is abort.
-        * Declining that gives the option to reset the superblock.
-        *
-        * Otherwise we get the chance to delete the journal, and
-        * failing that we abort because we can't handle this.
-        */
-       if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
-           fix_problem(ctx, PR_0_JOURNAL_UNSUPP_SUPER, pctx))
-               return EXT2_ET_CORRUPT_SUPERBLOCK;
-
-       if (e2fsck_journal_fix_bad_inode(ctx, pctx))
-               return EXT2_ET_UNSUPP_FEATURE;
-
-       return 0;
-}
-
-static int e2fsck_journal_load(journal_t *journal)
+static errcode_t e2fsck_journal_load(journal_t *journal)
 {
        e2fsck_t ctx = journal->j_dev;
        journal_superblock_t *jsb;
@@ -336,31 +379,33 @@ static int e2fsck_journal_load(journal_t *journal)
                
        case JFS_SUPERBLOCK_V2:
                journal->j_format_version = 2;
+               if (ntohl(jsb->s_nr_users) > 1) {
+                       fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
+                       return EXT2_ET_JOURNAL_UNSUPP_VERSION;
+               }
                break;
+
+       /*
+        * These should never appear in a journal super block, so if
+        * they do, the journal is badly corrupted.
+        */
+       case JFS_DESCRIPTOR_BLOCK:
+       case JFS_COMMIT_BLOCK:
+       case JFS_REVOKE_BLOCK:
+               return EXT2_ET_CORRUPT_SUPERBLOCK;
                
        /* If we don't understand the superblock major type, but there
         * is a magic number, then it is likely to be a new format we
         * just don't understand, so leave it alone. */
        default:
-               com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
-                       _("%s: journal has unrecognised format\n"),
-                       ctx->device_name);
-               return EXT2_ET_UNSUPP_FEATURE;
+               return EXT2_ET_JOURNAL_UNSUPP_VERSION;
        }
 
-       if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES)) {
-               com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
-                       _("%s: journal has incompatible features\n"),
-                       ctx->device_name);
+       if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
                return EXT2_ET_UNSUPP_FEATURE;
-       }
-               
-       if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES)) {
-               com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
-                       _("%s: journal has readonly-incompatible features\n"),
-                       ctx->device_name);
+       
+       if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
                return EXT2_ET_RO_UNSUPP_FEATURE;
-       }
 
        /* We have now checked whether we know enough about the journal
         * format to be able to proceed safely, so any other checks that
@@ -390,11 +435,17 @@ static int e2fsck_journal_load(journal_t *journal)
        return 0;
 }
 
-void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
-                               journal_t *journal)
+static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
+                                      journal_t *journal)
 {
        char *p;
-       
+       union {
+               uuid_t uuid;
+               __u32 val[4];
+       } u;
+       __u32 new_seq = 0;
+       int i;
+
        /* Leave a valid existing V1 superblock signature alone.
         * Anything unrecognisable we overwrite with a new V2
         * signature. */
@@ -413,29 +464,35 @@ void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
        jsb->s_blocksize = htonl(ctx->fs->blocksize);
        jsb->s_maxlen = htonl(journal->j_maxlen);
        jsb->s_first = htonl(1);
-       jsb->s_sequence = htonl(1);
 
-       /* In theory we should also re-zero the entire journal here.
-        * Initialising s_sequence to a random value would be a
-        * reasonable compromise. */
+       /* Initialize the journal sequence number so that there is "no"
+        * chance we will find old "valid" transactions in the journal.
+        * This avoids the need to zero the whole journal (slow to do,
+        * and risky when we are just recovering the filesystem).
+        */
+       uuid_generate(u.uuid);
+       for (i = 0; i < 4; i ++)
+               new_seq ^= u.val[i];
+       jsb->s_sequence = htonl(new_seq);
 
+       mark_buffer_dirty(journal->j_sb_buffer, 1);
        ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
 }
 
-static int e2fsck_journal_fix_corrupt_super(e2fsck_t ctx, journal_t *journal,
-                                           struct problem_context *pctx)
+static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
+                                                 journal_t *journal,
+                                                 struct problem_context *pctx)
 {
-       struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
-       int recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
+       struct ext2_super_block *sb = ctx->fs->super;
+       int recover = ctx->fs->super->s_feature_incompat &
+               EXT3_FEATURE_INCOMPAT_RECOVER;
 
        pctx->num = journal->j_inode->i_ino;
 
-       if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
+       if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
                if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
-                       journal_superblock_t *jsb = journal->j_superblock;
-
-                       e2fsck_journal_reset_super(ctx, jsb, journal);
-
+                       e2fsck_journal_reset_super(ctx, journal->j_superblock,
+                                                  journal);
                        journal->j_transaction_sequence = 1;
                        e2fsck_clear_recover(ctx, recover);
                        return 0;
@@ -447,11 +504,14 @@ static int e2fsck_journal_fix_corrupt_super(e2fsck_t ctx, journal_t *journal,
        return 0;
 }
 
-static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal, int reset)
+static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
+                                  int reset, int drop)
 {
        journal_superblock_t *jsb;
 
-       if (!(ctx->options & E2F_OPT_READONLY)) {
+       if (drop)
+               mark_buffer_clean(journal->j_sb_buffer);
+       else if (!(ctx->options & E2F_OPT_READONLY)) {
                jsb = journal->j_superblock;
                jsb->s_sequence = htonl(journal->j_transaction_sequence);
                if (reset)
@@ -460,26 +520,39 @@ static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal, int reset)
        }
        brelse(journal->j_sb_buffer);
 
+       if (ctx->journal_io) {
+               if (ctx->fs && ctx->fs->io != ctx->journal_io)
+                       io_channel_close(ctx->journal_io);
+               ctx->journal_io = 0;
+       }
+       
        if (journal->j_inode)
-               free(journal->j_inode);
+               ext2fs_free_mem((void **)&journal->j_inode);
        ext2fs_free_mem((void **)&journal);
 }
 
+/*
+ * This function makes sure that the superblock fields regarding the
+ * journal are consistent.
+ */
 int e2fsck_check_ext3_journal(e2fsck_t ctx)
 {
-       struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
+       struct ext2_super_block *sb = ctx->fs->super;
        journal_t *journal;
-       int recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
+       int recover = ctx->fs->super->s_feature_incompat &
+               EXT3_FEATURE_INCOMPAT_RECOVER;
        struct problem_context pctx;
-       int reset = 0;
+       int reset = 0, force_fsck = 0;
        int retval;
 
        /* If we don't have any journal features, don't do anything more */
-       if (!(s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
-               return 0;
+       if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
+           !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
+           uuid_is_null(sb->s_journal_uuid))
+               return 0;
 
        clear_problem_context(&pctx);
-       pctx.num = s->s_journal_inum;
+       pctx.num = sb->s_journal_inum;
 
        retval = e2fsck_get_journal(ctx, &journal);
        if (retval) {
@@ -490,9 +563,18 @@ int e2fsck_check_ext3_journal(e2fsck_t ctx)
 
        retval = e2fsck_journal_load(journal);
        if (retval) {
-               if (retval == EXT2_ET_CORRUPT_SUPERBLOCK)
-                       return e2fsck_journal_fix_corrupt_super(ctx, journal,
-                                                               &pctx);
+               if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
+                   ((retval == EXT2_ET_UNSUPP_FEATURE) &&
+                   (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
+                                 &pctx))) ||
+                   ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
+                   (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
+                                 &pctx))) ||
+                   ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
+                   (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
+                       retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
+                                                                 &pctx);
+               e2fsck_journal_release(ctx, journal, 0, 1);
                return retval;
        }
 
@@ -502,36 +584,58 @@ int e2fsck_check_ext3_journal(e2fsck_t ctx)
         * with -y, -n, or -p, only if a user isn't making up their mind.
         */
 no_has_journal:
-       if (!(s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
-               recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
+       if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
+               recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
                pctx.str = "inode";
                if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
                        if (recover &&
                            !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
                                goto no_has_journal;
-                       s->s_journal_inum = 0;
-                       e2fsck_clear_recover(ctx, recover);
+                       /*
+                        * Need a full fsck if we are releasing a
+                        * journal stored on a reserved inode.
+                        */
+                       force_fsck = recover ||
+                               (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
+                       /* Clear all of the journal fields */
+                       sb->s_journal_inum = 0;
+                       sb->s_journal_dev = 0;
+                       memset(sb->s_journal_uuid, 0,
+                              sizeof(sb->s_journal_uuid));
+                       e2fsck_clear_recover(ctx, force_fsck);
                } else if (!(ctx->options & E2F_OPT_READONLY)) {
-                       s->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
+                       sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
                        ext2fs_mark_super_dirty(ctx->fs);
                }
        }
 
-       if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
-           !(s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
+       if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
+           !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
            journal->j_superblock->s_start != 0) {
-               if (fix_problem(ctx, PR_0_JOURNAL_RESET_JOURNAL, &pctx))
+               if (fix_problem(ctx, PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
                        reset = 1;
-               /* I refuse to enable recovery for journal */
+                       sb->s_state &= ~EXT2_VALID_FS;
+                       ext2fs_mark_super_dirty(ctx->fs);
+               }
+               /*
+                * If the user answers no to the above question, we
+                * ignore the fact that journal apparently has data;
+                * accidentally replaying over valid data would be far
+                * worse than skipping a questionable recovery.
+                * 
+                * XXX should we abort with a fatal error here?  What
+                * will the ext3 kernel code do if a filesystem with
+                * !NEEDS_RECOVERY but with a non-zero
+                * journal->j_superblock->s_start is mounted?
+                */
        }
 
-       e2fsck_journal_release(ctx, journal, reset);
+       e2fsck_journal_release(ctx, journal, reset, 0);
        return retval;
 }
 
-static int recover_ext3_journal(e2fsck_t ctx)
+static errcode_t recover_ext3_journal(e2fsck_t ctx)
 {
-       ext2_filsys fs = ctx->fs;
        journal_t *journal;
        int retval;
 
@@ -541,119 +645,27 @@ static int recover_ext3_journal(e2fsck_t ctx)
 
        retval = e2fsck_journal_load(journal);
        if (retval)
-               return retval;
+               goto errout;
 
        retval = journal_init_revoke(journal, 1024);
        if (retval)
-               return retval;
+               goto errout;
        
        retval = -journal_recover(journal);
-       e2fsck_journal_release(ctx, journal, 1);
-       return retval;
-}
-
-
-#if 0
-#define TEMPLATE "/tmp/ext3.XXXXXX"
-
-/*
- * This function attempts to mount and unmount an ext3 filesystem,
- * which is a cheap way to force the kernel to run the journal and
- * handle the recovery for us.
- */
-static int recover_ext3_journal_via_mount(e2fsck_t ctx)
-{
-       ext2_filsys fs = ctx->fs;
-       char    *dirlist[] = {"/mnt","/lost+found","/tmp","/root","/boot",0};
-       errcode_t        retval, retval2;
-       int      count = 0;
-       char     template[] = TEMPLATE;
-       struct stat buf;
-       char    *tmpdir;
-
-       if (ctx->options & E2F_OPT_READONLY) {
-               printf("%s: won't do journal recovery while read-only\n",
-                      ctx->device_name);
-               return EXT2_ET_FILE_RO;
-       }
-
-       printf(_("%s: trying for ext3 kernel journal recovery\n"),
-              ctx->device_name);
-       /*
-        * First try to make a temporary directory.  This may fail if
-        * the root partition is still mounted read-only.
-        */
-newtemp:
-       tmpdir = mktemp(template);
-       if (tmpdir) {
-               jfs_debug(2, "trying %s as ext3 temp mount point\n", tmpdir);
-               if (mkdir(template, 0700)) {
-                       if (errno == EROFS) {
-                               tmpdir = NULL;
-                               template[0] = '\0';
-                       } else if (errno == EEXIST && count++ < 10) {
-                               strcpy(template, TEMPLATE);
-                               goto newtemp;
-                       }
-                       return errno;
-               }
-       }
-
-       /*
-        * OK, creating a temporary directory didn't work.
-        * Let's try a list of possible temporary mountpoints.
-        */
-       if (!tmpdir) {
-               dev_t   rootdev;
-               char    **cpp, *dir;
-
-               if (stat("/", &buf))
-                       return errno;
-
-               rootdev = buf.st_dev;
-
-               /*
-                * Check that dir is on the same device as root (no other
-                * filesystem is mounted there), and it's a directory.
-                */
-               for (cpp = dirlist; (dir = *cpp); cpp++)
-                       if (stat(dir, &buf) == 0 && buf.st_dev == rootdev &&
-                           S_ISDIR(buf.st_mode)) {
-                               tmpdir = dir;
-                               break;
-                       }
-       }
-
-       if (tmpdir) {
-               io_manager      io_ptr = fs->io->manager;
-               int             blocksize = fs->blocksize;
-
-               jfs_debug(2, "using %s for ext3 mount\n", tmpdir);
-               /* FIXME - need to handle loop devices here */
-               if (mount(ctx->device_name, tmpdir, "ext3", MNT_FL, NULL)) {
-                       retval = errno;
-                       com_err(ctx->program_name, errno,
-                               "when mounting %s", ctx->device_name);
-                       if (template[0])
-                               rmdir(tmpdir);
-                       return retval;
-               }
-               /*
-                * Now that it mounted cleanly, the filesystem will have been
-                * recovered, so we can now unmount it.
-                */
-               if (umount(tmpdir))
-                       return errno;
-
-               /*
-                * Remove the temporary directory, if it was created.
-                */
-               if (template[0])
-                       rmdir(tmpdir);
-               return 0;
+       if (retval)
+               goto errout;
+       
+       if (journal->j_superblock->s_errno) {
+               ctx->fs->super->s_state |= EXT2_ERROR_FS;
+               ext2fs_mark_super_dirty(ctx->fs);
+               journal->j_superblock->s_errno = 0;
+               mark_buffer_dirty(journal->j_sb_buffer, 1);
        }
+               
+errout:
+       e2fsck_journal_release(ctx, journal, 1, 0);
+       return retval;
 }
-#endif
 
 int e2fsck_run_ext3_journal(e2fsck_t ctx)
 {
@@ -668,6 +680,9 @@ int e2fsck_run_ext3_journal(e2fsck_t ctx)
                return EXT2_ET_FILE_RO;
        }
 
+       if (ctx->fs->flags & EXT2_FLAG_DIRTY)
+               ext2fs_flush(ctx->fs);  /* Force out any modifications */
+
        recover_retval = recover_ext3_journal(ctx);
        
        /*