Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / e2fsck / journal.c
index eebd22f..22f06e7 100644 (file)
@@ -12,6 +12,7 @@
  * any later version.
  */
 
+#include "config.h"
 #ifdef HAVE_SYS_MOUNT_H
 #include <sys/param.h>
 #include <sys/mount.h>
@@ -39,11 +40,61 @@ static int bh_count = 0;
  */
 #undef USE_INODE_IO
 
+/* Checksumming functions */
+static int e2fsck_journal_verify_csum_type(journal_t *j,
+                                          journal_superblock_t *jsb)
+{
+       if (!JFS_HAS_INCOMPAT_FEATURE(j, JFS_FEATURE_INCOMPAT_CSUM_V2))
+               return 1;
+
+       return jsb->s_checksum_type == JBD2_CRC32C_CHKSUM;
+}
+
+static __u32 e2fsck_journal_sb_csum(journal_superblock_t *jsb)
+{
+       __u32 crc, old_crc;
+
+       old_crc = jsb->s_checksum;
+       jsb->s_checksum = 0;
+       crc = ext2fs_crc32c_le(~0, (unsigned char *)jsb,
+                              sizeof(journal_superblock_t));
+       jsb->s_checksum = old_crc;
+
+       return crc;
+}
+
+static int e2fsck_journal_sb_csum_verify(journal_t *j,
+                                        journal_superblock_t *jsb)
+{
+       __u32 provided, calculated;
+
+       if (!JFS_HAS_INCOMPAT_FEATURE(j, JFS_FEATURE_INCOMPAT_CSUM_V2))
+               return 1;
+
+       provided = ext2fs_be32_to_cpu(jsb->s_checksum);
+       calculated = e2fsck_journal_sb_csum(jsb);
+
+       return provided == calculated;
+}
+
+static errcode_t e2fsck_journal_sb_csum_set(journal_t *j,
+                                           journal_superblock_t *jsb)
+{
+       __u32 crc;
+
+       if (!JFS_HAS_INCOMPAT_FEATURE(j, JFS_FEATURE_INCOMPAT_CSUM_V2))
+               return 0;
+
+       crc = e2fsck_journal_sb_csum(jsb);
+       jsb->s_checksum = ext2fs_cpu_to_be32(crc);
+       return 0;
+}
+
 /* 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 journal_bmap(journal_t *journal, blk_t block, unsigned long *phys)
+int journal_bmap(journal_t *journal, blk64_t block, unsigned long long *phys)
 {
 #ifdef USE_INODE_IO
        *phys = block;
@@ -51,35 +102,41 @@ int journal_bmap(journal_t *journal, blk_t block, unsigned long *phys)
 #else
        struct inode    *inode = journal->j_inode;
        errcode_t       retval;
-       blk_t           pblk;
+       blk64_t         pblk;
 
        if (!inode) {
                *phys = block;
                return 0;
        }
 
-       retval= ext2fs_bmap(inode->i_ctx->fs, inode->i_ino, 
-                           &inode->i_ext2, NULL, 0, block, &pblk);
+       retval= ext2fs_bmap2(inode->i_ctx->fs, inode->i_ino,
+                            &inode->i_ext2, NULL, 0, block, 0, &pblk);
        *phys = pblk;
-       return (retval);
+       return (int) retval;
 #endif
 }
 
-struct buffer_head *getblk(kdev_t kdev, blk_t blocknr, int blocksize)
+struct buffer_head *getblk(kdev_t kdev, blk64_t blocknr, int blocksize)
 {
        struct buffer_head *bh;
+       int bufsize = sizeof(*bh) + kdev->k_ctx->fs->blocksize -
+               sizeof(bh->b_data);
 
-       bh = e2fsck_allocate_memory(kdev->k_ctx, sizeof(*bh), "block buffer");
+       bh = e2fsck_allocate_memory(kdev->k_ctx, bufsize, "block buffer");
        if (!bh)
                return NULL;
 
-       jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
-                 (unsigned long) blocknr, blocksize, ++bh_count);
+#ifdef CONFIG_JBD_DEBUG
+       if (journal_enable_debug >= 3)
+               bh_count++;
+#endif
+       jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n",
+                 (unsigned long long) blocknr, blocksize, bh_count);
 
        bh->b_ctx = kdev->k_ctx;
        if (kdev->k_dev == K_DEV_FS)
                bh->b_io = kdev->k_ctx->fs->io;
-       else 
+       else
                bh->b_io = kdev->k_ctx->journal_io;
        bh->b_size = blocksize;
        bh->b_blocknr = blocknr;
@@ -93,7 +150,7 @@ void sync_blockdev(kdev_t kdev)
 
        if (kdev->k_dev == K_DEV_FS)
                io = kdev->k_ctx->fs->io;
-       else 
+       else
                io = kdev->k_ctx->journal_io;
 
        io_channel_flush(io);
@@ -101,44 +158,45 @@ void sync_blockdev(kdev_t kdev)
 
 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
 {
-       int retval;
+       errcode_t retval;
        struct buffer_head *bh;
 
        for (; nr > 0; --nr) {
                bh = *bhp++;
                if (rw == READ && !bh->b_uptodate) {
-                       jfs_debug(3, "reading block %lu/%p\n", 
-                                 (unsigned long) bh->b_blocknr, (void *) bh);
-                       retval = io_channel_read_blk(bh->b_io, 
+                       jfs_debug(3, "reading block %llu/%p\n",
+                                 bh->b_blocknr, (void *) bh);
+                       retval = io_channel_read_blk64(bh->b_io,
                                                     bh->b_blocknr,
                                                     1, bh->b_data);
                        if (retval) {
                                com_err(bh->b_ctx->device_name, retval,
-                                       "while reading block %lu\n", 
-                                       (unsigned long) bh->b_blocknr);
-                               bh->b_err = retval;
+                                       "while reading block %llu\n",
+                                       bh->b_blocknr);
+                               bh->b_err = (int) retval;
                                continue;
                        }
                        bh->b_uptodate = 1;
                } else if (rw == WRITE && bh->b_dirty) {
-                       jfs_debug(3, "writing block %lu/%p\n", 
-                                 (unsigned long) bh->b_blocknr, (void *) bh);
-                       retval = io_channel_write_blk(bh->b_io, 
+                       jfs_debug(3, "writing block %llu/%p\n",
+                                 bh->b_blocknr,
+                                 (void *) bh);
+                       retval = io_channel_write_blk64(bh->b_io,
                                                      bh->b_blocknr,
                                                      1, bh->b_data);
                        if (retval) {
                                com_err(bh->b_ctx->device_name, retval,
-                                       "while writing block %lu\n", 
-                                       (unsigned long) bh->b_blocknr);
-                               bh->b_err = retval;
+                                       "while writing block %llu\n",
+                                       bh->b_blocknr);
+                               bh->b_err = (int) retval;
                                continue;
                        }
                        bh->b_dirty = 0;
                        bh->b_uptodate = 1;
                } else {
-                       jfs_debug(3, "no-op %s for block %lu\n",
-                                 rw == READ ? "read" : "write", 
-                                 (unsigned long) bh->b_blocknr);
+                       jfs_debug(3, "no-op %s for block %llu\n",
+                                 rw == READ ? "read" : "write",
+                                 bh->b_blocknr);
                }
        }
 }
@@ -157,8 +215,8 @@ 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",
-                 (unsigned long) bh->b_blocknr, (void *) bh, --bh_count);
+       jfs_debug(3, "freeing block %llu/%p (total %d)\n",
+                 bh->b_blocknr, (void *) bh, --bh_count);
        ext2fs_free_mem(&bh);
 }
 
@@ -197,19 +255,19 @@ struct process_block_struct {
 };
 
 static int process_journal_block(ext2_filsys fs,
-                                blk_t  *block_nr,
+                                blk64_t        *block_nr,
                                 e2_blkcnt_t blockcnt,
-                                blk_t ref_block EXT2FS_ATTR((unused)),
+                                blk64_t ref_block EXT2FS_ATTR((unused)),
                                 int ref_offset EXT2FS_ATTR((unused)),
                                 void *priv_data)
 {
        struct process_block_struct *p;
-       blk_t   blk = *block_nr;
+       blk64_t blk = *block_nr;
 
        p = (struct process_block_struct *) priv_data;
 
-       if (blk < fs->super->s_first_data_block ||
-           blk >= fs->super->s_blocks_count)
+       if (!blk || blk < fs->super->s_first_data_block ||
+           blk >= ext2fs_blocks_count(fs->super))
                return BLOCK_ABORT;
 
        if (blockcnt >= 0)
@@ -230,7 +288,7 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
        journal_t               *journal = NULL;
        errcode_t               retval = 0;
        io_manager              io_ptr = 0;
-       unsigned long           start = 0;
+       unsigned long long      start = 0;
        int                     ext_journal = 0;
        int                     tried_backup_jnl = 0;
 
@@ -280,8 +338,9 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
                            tried_backup_jnl)
                                goto errout;
                        memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
-                       memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks, 
+                       memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
                               EXT2_N_BLOCKS*4);
+                       j_inode->i_ext2.i_size_high = sb->s_jnl_blocks[15];
                        j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
                        j_inode->i_ext2.i_links_count = 1;
                        j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
@@ -295,17 +354,17 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
                        retval = EXT2_ET_NO_JOURNAL;
                        goto try_backup_journal;
                }
-               if (j_inode->i_ext2.i_size / journal->j_blocksize <
+               if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize <
                    JFS_MIN_JOURNAL_BLOCKS) {
                        retval = EXT2_ET_JOURNAL_TOO_SMALL;
                        goto try_backup_journal;
                }
                pb.last_block = -1;
-               retval = ext2fs_block_iterate2(ctx->fs, j_inode->i_ino,
-                                              BLOCK_FLAG_HOLE, 0, 
+               retval = ext2fs_block_iterate3(ctx->fs, j_inode->i_ino,
+                                              BLOCK_FLAG_HOLE, 0,
                                               process_journal_block, &pb);
-               if ((pb.last_block+1) * ctx->fs->blocksize < 
-                   j_inode->i_ext2.i_size) {
+               if ((pb.last_block + 1) * ctx->fs->blocksize <
+                   (int) EXT2_I_SIZE(&j_inode->i_ext2)) {
                        retval = EXT2_ET_JOURNAL_TOO_SMALL;
                        goto try_backup_journal;
                }
@@ -316,7 +375,8 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
                                goto errout;
                }
 
-               journal->j_maxlen = j_inode->i_ext2.i_size / journal->j_blocksize;
+               journal->j_maxlen = EXT2_I_SIZE(&j_inode->i_ext2) /
+                       journal->j_blocksize;
 
 #ifdef USE_INODE_IO
                retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
@@ -329,7 +389,7 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 #else
                journal->j_inode = j_inode;
                ctx->journal_io = ctx->fs->io;
-               if ((retval = journal_bmap(journal, 0, &start)) != 0)
+               if ((retval = (errcode_t) journal_bmap(journal, 0, &start)) != 0)
                        goto errout;
 #endif
        } else {
@@ -362,14 +422,27 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
 #ifndef USE_INODE_IO
        if (ext_journal)
 #endif
-               retval = io_ptr->open(journal_name, IO_FLAG_RW,
+       {
+               int flags = IO_FLAG_RW;
+               if (!(ctx->mount_flags & EXT2_MF_ISROOT &&
+                     ctx->mount_flags & EXT2_MF_READONLY))
+                       flags |= IO_FLAG_EXCLUSIVE;
+               if ((ctx->mount_flags & EXT2_MF_READONLY) &&
+                   (ctx->options & E2F_OPT_FORCE))
+                       flags &= ~IO_FLAG_EXCLUSIVE;
+
+
+               retval = io_ptr->open(journal_name, flags,
                                      &ctx->journal_io);
+       }
        if (retval)
                goto errout;
 
        io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
 
        if (ext_journal) {
+               blk64_t maxlen;
+
                if (ctx->fs->blocksize == 1024)
                        start = 1;
                bh = getblk(dev_journal, start, ctx->fs->blocksize);
@@ -386,7 +459,7 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
                       sizeof(jsuper));
                brelse(bh);
 #ifdef WORDS_BIGENDIAN
-               if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) 
+               if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
                        ext2fs_swap_super(&jsuper);
 #endif
                if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
@@ -403,7 +476,8 @@ static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
                        goto errout;
                }
 
-               journal->j_maxlen = jsuper.s_blocks_count;
+               maxlen = ext2fs_blocks_count(&jsuper);
+               journal->j_maxlen = (maxlen < 1ULL << 32) ? maxlen : (1ULL << 32) - 1;
                start++;
        }
 
@@ -454,6 +528,7 @@ static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
                        sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
                        sb->s_journal_inum = 0;
                        ctx->flags |= E2F_FLAG_JOURNAL_INODE;
+                       ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
                        e2fsck_clear_recover(ctx, 1);
                        return 0;
                }
@@ -515,7 +590,7 @@ static errcode_t e2fsck_journal_load(journal_t *journal)
                    jsb->s_nr_users)
                        clear_v2_journal_fields(journal);
                break;
-               
+
        case JFS_SUPERBLOCK_V2:
                journal->j_format_version = 2;
                if (ntohl(jsb->s_nr_users) > 1 &&
@@ -535,7 +610,7 @@ static errcode_t e2fsck_journal_load(journal_t *journal)
        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. */
@@ -545,10 +620,19 @@ static errcode_t e2fsck_journal_load(journal_t *journal)
 
        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))
                return EXT2_ET_RO_UNSUPP_FEATURE;
 
+       /* Checksum v1 and v2 are mutually exclusive features. */
+       if (JFS_HAS_INCOMPAT_FEATURE(journal, JFS_FEATURE_INCOMPAT_CSUM_V2) &&
+           JFS_HAS_COMPAT_FEATURE(journal, JFS_FEATURE_COMPAT_CHECKSUM))
+               return EXT2_ET_CORRUPT_SUPERBLOCK;
+
+       if (!e2fsck_journal_verify_csum_type(journal, jsb) ||
+           !e2fsck_journal_sb_csum_verify(journal, jsb))
+               return EXT2_ET_CORRUPT_SUPERBLOCK;
+
        /* We have now checked whether we know enough about the journal
         * format to be able to proceed safely, so any other checks that
         * fail we should attempt to recover from. */
@@ -591,7 +675,7 @@ static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
        /* Leave a valid existing V1 superblock signature alone.
         * Anything unrecognisable we overwrite with a new V2
         * signature. */
-       
+
        if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
            jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
                jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
@@ -599,7 +683,7 @@ static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
        }
 
        /* Zero out everything else beyond the superblock header */
-       
+
        p = ((char *) jsb) + sizeof(journal_header_t);
        memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
 
@@ -616,6 +700,7 @@ static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
        for (i = 0; i < 4; i ++)
                new_seq ^= u.val[i];
        jsb->s_sequence = htonl(new_seq);
+       e2fsck_journal_sb_csum_set(journal, jsb);
 
        mark_buffer_dirty(journal->j_sb_buffer);
        ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
@@ -656,6 +741,7 @@ static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
                jsb->s_sequence = htonl(journal->j_transaction_sequence);
                if (reset)
                        jsb->s_start = 0; /* this marks the journal as empty */
+               e2fsck_journal_sb_csum_set(journal, jsb);
                mark_buffer_dirty(journal->j_sb_buffer);
        }
        brelse(journal->j_sb_buffer);
@@ -665,7 +751,7 @@ static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
                        io_channel_close(ctx->journal_io);
                ctx->journal_io = 0;
        }
-       
+
 #ifndef USE_INODE_IO
        if (journal->j_inode)
                ext2fs_free_mem(&journal->j_inode);
@@ -679,7 +765,7 @@ static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
  * This function makes sure that the superblock fields regarding the
  * journal are consistent.
  */
-int e2fsck_check_ext3_journal(e2fsck_t ctx)
+errcode_t e2fsck_check_ext3_journal(e2fsck_t ctx)
 {
        struct ext2_super_block *sb = ctx->fs->super;
        journal_t *journal;
@@ -688,7 +774,7 @@ int e2fsck_check_ext3_journal(e2fsck_t ctx)
        struct problem_context pctx;
        problem_t problem;
        int reset = 0, force_fsck = 0;
-       int retval;
+       errcode_t retval;
 
        /* If we don't have any journal features, don't do anything more */
        if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
@@ -753,6 +839,7 @@ no_has_journal:
                        e2fsck_clear_recover(ctx, force_fsck);
                } else if (!(ctx->options & E2F_OPT_READONLY)) {
                        sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
+                       ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
                        ext2fs_mark_super_dirty(ctx->fs);
                }
        }
@@ -782,7 +869,7 @@ no_has_journal:
                 * 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
@@ -790,14 +877,31 @@ no_has_journal:
                 */
        }
 
+       /*
+        * If we don't need to do replay the journal, check to see if
+        * the journal's errno is set; if so, we need to mark the file
+        * system as being corrupt and clear the journal's s_errno.
+        */
+       if (!(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
+           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;
+               e2fsck_journal_sb_csum_set(journal, journal->j_superblock);
+               mark_buffer_dirty(journal->j_sb_buffer);
+       }
+
        e2fsck_journal_release(ctx, journal, reset, 0);
        return retval;
 }
 
 static errcode_t recover_ext3_journal(e2fsck_t ctx)
 {
+       struct problem_context  pctx;
        journal_t *journal;
-       int retval;
+       errcode_t retval;
+
+       clear_problem_context(&pctx);
 
        journal_init_revoke_caches();
        retval = e2fsck_get_journal(ctx, &journal);
@@ -811,18 +915,18 @@ static errcode_t recover_ext3_journal(e2fsck_t ctx)
        retval = journal_init_revoke(journal, 1024);
        if (retval)
                goto errout;
-       
+
        retval = -journal_recover(journal);
        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;
+
+       if (journal->j_failed_commit) {
+               pctx.ino = journal->j_failed_commit;
+               fix_problem(ctx, PR_0_JNL_TXN_CORRUPT, &pctx);
+               journal->j_superblock->s_errno = -EINVAL;
                mark_buffer_dirty(journal->j_sb_buffer);
        }
-               
+
 errout:
        journal_destroy_revoke(journal);
        journal_destroy_revoke_caches();
@@ -830,11 +934,13 @@ errout:
        return retval;
 }
 
-int e2fsck_run_ext3_journal(e2fsck_t ctx)
+errcode_t e2fsck_run_ext3_journal(e2fsck_t ctx)
 {
        io_manager io_ptr = ctx->fs->io->manager;
        int blocksize = ctx->fs->blocksize;
        errcode_t       retval, recover_retval;
+       io_stats        stats = 0;
+       unsigned long long kbytes_written = 0;
 
        printf(_("%s: recovering journal\n"), ctx->device_name);
        if (ctx->options & E2F_OPT_READONLY) {
@@ -847,16 +953,22 @@ int e2fsck_run_ext3_journal(e2fsck_t ctx)
                ext2fs_flush(ctx->fs);  /* Force out any modifications */
 
        recover_retval = recover_ext3_journal(ctx);
-       
+
        /*
         * Reload the filesystem context to get up-to-date data from disk
         * because journal recovery will change the filesystem under us.
         */
-       ext2fs_close(ctx->fs);
+       if (ctx->fs->super->s_kbytes_written &&
+           ctx->fs->io->manager->get_stats)
+               ctx->fs->io->manager->get_stats(ctx->fs->io, &stats);
+       if (stats && stats->bytes_written)
+               kbytes_written = stats->bytes_written >> 10;
+
+       ext2fs_mmp_stop(ctx->fs);
+       ext2fs_free(ctx->fs);
        retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
                             ctx->superblock, blocksize, io_ptr,
                             &ctx->fs);
-
        if (retval) {
                com_err(ctx->program_name, retval,
                        _("while trying to re-open %s"),
@@ -866,10 +978,17 @@ int e2fsck_run_ext3_journal(e2fsck_t ctx)
        ctx->fs->priv_data = ctx;
        ctx->fs->now = ctx->now;
        ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
+       ctx->fs->super->s_kbytes_written += kbytes_written;
 
        /* Set the superblock flags */
-       e2fsck_clear_recover(ctx, recover_retval);
-       return recover_retval;
+       e2fsck_clear_recover(ctx, recover_retval != 0);
+
+       /*
+        * Do one last sanity check, and propagate journal->s_errno to
+        * the EXT2_ERROR_FS flag in the fs superblock if needed.
+        */
+       retval = e2fsck_check_ext3_journal(ctx);
+       return retval ? retval : recover_retval;
 }
 
 /*
@@ -888,8 +1007,9 @@ void e2fsck_move_ext3_journal(e2fsck_t ctx)
        ext2_ino_t              ino;
        errcode_t               retval;
        const char * const *    cpp;
-       int                     group, mount_flags;
-       
+       dgrp_t                  group;
+       int                     mount_flags;
+
        clear_problem_context(&pctx);
 
        /*
@@ -916,6 +1036,7 @@ void e2fsck_move_ext3_journal(e2fsck_t ctx)
                if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
                        memcpy(sb->s_jnl_blocks, inode.i_block,
                               EXT2_N_BLOCKS*4);
+                       sb->s_jnl_blocks[15] = inode.i_size_high;
                        sb->s_jnl_blocks[16] = inode.i_size;
                        sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
                        ext2fs_mark_super_dirty(fs);
@@ -928,7 +1049,7 @@ void e2fsck_move_ext3_journal(e2fsck_t ctx)
         */
        if (sb->s_journal_inum == EXT2_JOURNAL_INO)
                return;
-       
+
        /*
         * The journal inode had better have only one link and not be readable.
         */
@@ -964,7 +1085,7 @@ void e2fsck_move_ext3_journal(e2fsck_t ctx)
        pctx.str = *cpp;
        if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
                return;
-               
+
        /*
         * OK, we've done all the checks, let's actually move the
         * journal inode.  Errors at this point mean we need to force
@@ -983,9 +1104,10 @@ void e2fsck_move_ext3_journal(e2fsck_t ctx)
                goto err_out;
 
        group = ext2fs_group_of_ino(fs, ino);
-       ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
+       ext2fs_unmark_inode_bitmap2(fs->inode_map, ino);
        ext2fs_mark_ib_dirty(fs);
-       fs->group_desc[group].bg_free_inodes_count++;
+       ext2fs_bg_free_inodes_count_set(fs, group, ext2fs_bg_free_inodes_count(fs, group) + 1);
+       ext2fs_group_desc_csum_set(fs, group);
        fs->super->s_free_inodes_count++;
        return;
 
@@ -1017,8 +1139,10 @@ int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
        if (!journal_name)
                return 0;
 
-       if (stat(journal_name, &st) < 0)
+       if (stat(journal_name, &st) < 0) {
+               free(journal_name);
                return 0;
+       }
 
        if (st.st_rdev != sb->s_journal_dev) {
                clear_problem_context(&pctx);