Whamcloud - gitweb
dumpe2fs: display block bitmap checksum
[tools/e2fsprogs.git] / e2fsck / pass1.c
index ee57497..0aa9356 100644 (file)
@@ -1,13 +1,13 @@
 /*
  * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
- * 
+ *
  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
  *
  * %Begin-Header%
  * This file may be redistributed under the terms of the GNU Public
  * License.
  * %End-Header%
- * 
+ *
  * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
  * and applies the following tests to each inode:
  *
@@ -38,6 +38,7 @@
  */
 
 #define _GNU_SOURCE 1 /* get strnlen() */
+#include "config.h"
 #include <string.h>
 #include <time.h>
 #ifdef HAVE_ERRNO_H
 #define _INLINE_ inline
 #endif
 
-static int process_block(ext2_filsys fs, blk_t *blocknr,
-                        e2_blkcnt_t blockcnt, blk_t ref_blk, 
+static int process_block(ext2_filsys fs, blk64_t       *blocknr,
+                        e2_blkcnt_t blockcnt, blk64_t ref_blk,
                         int ref_offset, void *priv_data);
-static int process_bad_block(ext2_filsys fs, blk_t *block_nr,
-                            e2_blkcnt_t blockcnt, blk_t ref_blk,
+static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
+                            e2_blkcnt_t blockcnt, blk64_t ref_blk,
                             int ref_offset, void *priv_data);
 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                         char *block_buf);
@@ -72,19 +73,21 @@ static void process_inodes(e2fsck_t ctx, char *block_buf);
 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
                                  dgrp_t group, void * priv_data);
-static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount, 
+static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
                                    char *block_buf, int adjust_sign);
-/* static char *describe_illegal_block(ext2_filsys fs, blk_t block); */
+/* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
 
 struct process_block_struct {
        ext2_ino_t      ino;
        unsigned        is_dir:1, is_reg:1, clear:1, suppress:1,
                                fragmented:1, compressed:1, bbcheck:1;
-       blk_t           num_blocks;
-       blk_t           max_blocks;
+       blk64_t         num_blocks;
+       blk64_t         max_blocks;
        e2_blkcnt_t     last_block;
+       e2_blkcnt_t     last_init_lblock;
+       e2_blkcnt_t     last_db_block;
        int             num_illegal_blocks;
-       blk_t           previous_block;
+       blk64_t         previous_block;
        struct ext2_inode *inode;
        struct problem_context *pctx;
        ext2fs_block_bitmap fs_meta_blocks;
@@ -126,9 +129,9 @@ static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
  *
  * Note: this routine is now also used to check FIFO's and Sockets,
  * since they have the same requirement; the i_block fields should be
- * zero. 
+ * zero.
  */
-int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)), 
+int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
                                    struct ext2_inode *inode)
 {
        int     i;
@@ -152,7 +155,7 @@ int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
         * the kernel has been fixed we can change this...
         */
        if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
-               for (i=4; i < EXT2_N_BLOCKS; i++) 
+               for (i=4; i < EXT2_N_BLOCKS; i++)
                        if (inode->i_block[i])
                                return 0;
        }
@@ -168,7 +171,7 @@ int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
 {
        unsigned int len;
        int i;
-       blk_t   blocks;
+       blk64_t blocks;
        ext2_extent_handle_t    handle;
        struct ext2_extent_info info;
        struct ext2fs_extent    extent;
@@ -180,7 +183,7 @@ int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
        if (inode->i_flags & EXT4_EXTENTS_FL) {
                if (inode->i_size > fs->blocksize)
                        return 0;
-               if (ext2fs_extent_open(fs, ino, &handle))
+               if (ext2fs_extent_open2(fs, ino, inode, &handle))
                        return 0;
                i = 0;
                if (ext2fs_extent_get_info(handle, &info) ||
@@ -191,7 +194,7 @@ int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
                    (extent.e_lblk != 0) ||
                    (extent.e_len != 1) ||
                    (extent.e_pblk < fs->super->s_first_data_block) ||
-                   (extent.e_pblk >= fs->super->s_blocks_count))
+                   (extent.e_pblk >= ext2fs_blocks_count(fs->super)))
                        goto exit_extent;
                i = 1;
        exit_extent:
@@ -199,19 +202,19 @@ int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
                return i;
        }
 
-       blocks = ext2fs_inode_data_blocks(fs, inode);
+       blocks = ext2fs_inode_data_blocks2(fs, inode);
        if (blocks) {
                if ((inode->i_size >= fs->blocksize) ||
                    (blocks != fs->blocksize >> 9) ||
                    (inode->i_block[0] < fs->super->s_first_data_block) ||
-                   (inode->i_block[0] >= fs->super->s_blocks_count))
+                   (inode->i_block[0] >= ext2fs_blocks_count(fs->super)))
                        return 0;
 
                for (i = 1; i < EXT2_N_BLOCKS; i++)
                        if (inode->i_block[i])
                                return 0;
 
-               if (io_channel_read_blk(fs->io, inode->i_block[0], 1, buf))
+               if (io_channel_read_blk64(fs->io, inode->i_block[0], 1, buf))
                        return 0;
 
                len = strnlen(buf, fs->blocksize);
@@ -254,18 +257,18 @@ static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
 {
        struct ext2_inode *inode = pctx->inode;
-       
-       if ((inode->i_size == 0) && (inode->i_size_high == 0))
+
+       if (EXT2_I_SIZE(inode) == 0)
                return;
-       
+
        if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
                return;
-       
+
        inode->i_size = 0;
        inode->i_size_high = 0;
        e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
 }
-       
+
 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
 {
        struct ext2_super_block *sb = ctx->fs->super;
@@ -286,14 +289,14 @@ static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
        /* scan all entry's headers first */
 
        /* take finish entry 0UL into account */
-       remain = storage_size - sizeof(__u32); 
+       remain = storage_size - sizeof(__u32);
 
        while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
                __u32 hash;
 
                /* header eats this space */
                remain -= sizeof(struct ext2_ext_attr_entry);
-               
+
                /* is attribute name valid? */
                if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
                        pctx->num = entry->e_name_len;
@@ -362,11 +365,11 @@ static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
 #if 0
        printf("inode #%u, i_extra_size %d\n", pctx->ino,
                        inode->i_extra_isize);
-#endif 
-       /* i_extra_isize must cover i_extra_isize + i_pad1 at least */
-       min = sizeof(inode->i_extra_isize) + sizeof(inode->i_pad1);
+#endif
+       /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
+       min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
        max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
-       /* 
+       /*
         * For now we will allow i_extra_isize to be 0, but really
         * implementations should never allow i_extra_isize to be 0
         */
@@ -388,7 +391,7 @@ static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
        }
 }
 
-/* 
+/*
  * Check to see if the inode might really be a directory, despite i_mode
  *
  * This is a lot of complexity for something for which I'm not really
@@ -401,64 +404,113 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
 {
        struct ext2_inode *inode = pctx->inode;
        struct ext2_dir_entry   *dirent;
-       const char              *old_op;
        errcode_t               retval;
-       blk_t                   blk;
-       int                     i, not_device = 0;
+       blk64_t                 blk;
+       unsigned int            i, rec_len, not_device = 0;
+       int                     extent_fs;
 
+       /*
+        * If the mode looks OK, we believe it.  If the first block in
+        * the i_block array is 0, this cannot be a directory. If the
+        * inode is extent-mapped, it is still the case that the latter
+        * cannot be 0 - the magic number in the extent header would make
+        * it nonzero.
+        */
        if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
            LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
                return;
 
-       for (i=0; i < EXT2_N_BLOCKS; i++) {
-               blk = inode->i_block[i];
-               if (!blk)
-                       continue;
-               if (i >= 4)
-                       not_device++;
+       /* 
+        * Check the block numbers in the i_block array for validity:
+        * zero blocks are skipped (but the first one cannot be zero -
+        * see above), other blocks are checked against the first and
+        * max data blocks (from the the superblock) and against the
+        * block bitmap. Any invalid block found means this cannot be
+        * a directory.
+        * 
+        * If there are non-zero blocks past the fourth entry, then
+        * this cannot be a device file: we remember that for the next
+        * check.
+        *
+        * For extent mapped files, we don't do any sanity checking:
+        * just try to get the phys block of logical block 0 and run
+        * with it.
+        */
+
+       extent_fs = (ctx->fs->super->s_feature_incompat &
+                    EXT3_FEATURE_INCOMPAT_EXTENTS);
+       if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
+               /* extent mapped */
+               if  (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
+                                &blk))
+                       return;
+               /* device files are never extent mapped */
+               not_device++;
+       } else {
+               for (i=0; i < EXT2_N_BLOCKS; i++) {
+                       blk = inode->i_block[i];
+                       if (!blk)
+                               continue;
+                       if (i >= 4)
+                               not_device++;
 
-               if (blk < ctx->fs->super->s_first_data_block ||
-                   blk >= ctx->fs->super->s_blocks_count ||
-                   ext2fs_fast_test_block_bitmap(ctx->block_found_map, blk))
-                       return; /* Invalid block, can't be dir */
+                       if (blk < ctx->fs->super->s_first_data_block ||
+                           blk >= ext2fs_blocks_count(ctx->fs->super) ||
+                           ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
+                                                          blk))
+                               return; /* Invalid block, can't be dir */
+               }
+               blk = inode->i_block[0];
        }
 
-       if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) && 
+       /*
+        * If the mode says this is a device file and the i_links_count field
+        * is sane and we have not ruled it out as a device file previously,
+        * we declare it a device file, not a directory.
+        */
+       if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
            (inode->i_links_count == 1) && !not_device)
                return;
 
-       old_op = ehandler_operation(_("reading directory block"));
-       retval = ext2fs_read_dir_block(ctx->fs, inode->i_block[0], buf);
+       /* read the first block */
+       ehandler_operation(_("reading directory block"));
+       retval = ext2fs_read_dir_block3(ctx->fs, blk, buf, 0);
        ehandler_operation(0);
        if (retval)
                return;
 
        dirent = (struct ext2_dir_entry *) buf;
+       retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
+       if (retval)
+               return;
        if (((dirent->name_len & 0xFF) != 1) ||
            (dirent->name[0] != '.') ||
            (dirent->inode != pctx->ino) ||
-           (dirent->rec_len < 12) ||
-           (dirent->rec_len % 4) ||
-           (dirent->rec_len >= ctx->fs->blocksize - 12))
+           (rec_len < 12) ||
+           (rec_len % 4) ||
+           (rec_len >= ctx->fs->blocksize - 12))
                return;
 
-       dirent = (struct ext2_dir_entry *) (buf + dirent->rec_len);
+       dirent = (struct ext2_dir_entry *) (buf + rec_len);
+       retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
+       if (retval)
+               return;
        if (((dirent->name_len & 0xFF) != 2) ||
            (dirent->name[0] != '.') ||
            (dirent->name[1] != '.') ||
-           (dirent->rec_len < 12) ||
-           (dirent->rec_len % 4))
+           (rec_len < 12) ||
+           (rec_len % 4))
                return;
 
        if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
                inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
-               e2fsck_write_inode_full(ctx, pctx->ino, inode, 
-                                       EXT2_INODE_SIZE(ctx->fs->super), 
+               e2fsck_write_inode_full(ctx, pctx->ino, inode,
+                                       EXT2_INODE_SIZE(ctx->fs->super),
                                        "check_is_really_dir");
        }
 }
 
-extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags, 
+extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
                                    ext2_icount_t *ret)
 {
        unsigned int            threshold;
@@ -489,12 +541,46 @@ extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
                *ret = 0;
 }
 
+static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
+                                           e2fsck_t ctx,
+                                           struct problem_context *pctx)
+{
+       errcode_t retval;
+       struct ext2_inode_large inode;
+
+       /*
+        * Reread inode.  If we don't see checksum error, then this inode
+        * has been fixed elsewhere.
+        */
+       retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
+                                       sizeof(inode));
+       if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
+               return retval;
+       if (!retval)
+               return 0;
+
+       /*
+        * Checksum still doesn't match.  That implies that the inode passes
+        * all the sanity checks, so maybe the checksum is simply corrupt.
+        * See if the user will go for fixing that.
+        */
+       if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
+               return 0;
+
+       retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
+                                        sizeof(inode));
+       if (retval)
+               return retval;
+
+       return 0;
+}
+
 void e2fsck_pass1(e2fsck_t ctx)
 {
        int     i;
        __u64   max_sizes;
        ext2_filsys fs = ctx->fs;
-       ext2_ino_t      ino;
+       ext2_ino_t      ino = 0;
        struct ext2_inode *inode;
        ext2_inode_scan scan;
        char            *block_buf;
@@ -506,13 +592,13 @@ void e2fsck_pass1(e2fsck_t ctx)
        struct          scan_callback_struct scan_struct;
        struct ext2_super_block *sb = ctx->fs->super;
        const char      *old_op;
+       unsigned int    save_type;
        int             imagic_fs, extent_fs;
        int             busted_fs_time = 0;
        int             inode_size;
-       
-#ifdef RESOURCE_TRACK
+       int             failed_csum = 0;
+
        init_resource_track(&rtrack, ctx->fs->io);
-#endif
        clear_problem_context(&pctx);
 
        if (!(ctx->options & E2F_OPT_PREEN))
@@ -534,7 +620,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
                max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
                max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
-               max_sizes = (max_sizes * (1UL << i)) - 1;
+               max_sizes = (max_sizes * (1UL << i));
                ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
        }
 #undef EXT2_BPP
@@ -545,32 +631,38 @@ void e2fsck_pass1(e2fsck_t ctx)
        /*
         * Allocate bitmaps structures
         */
-       pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
-                                             &ctx->inode_used_map);
+       pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
+                                                   EXT2FS_BMAP64_RBTREE,
+                                                   "inode_used_map",
+                                                   &ctx->inode_used_map);
        if (pctx.errcode) {
                pctx.num = 1;
                fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
-       pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
-                               _("directory inode map"), &ctx->inode_dir_map);
+       pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
+                       _("directory inode map"),
+                       EXT2FS_BMAP64_AUTODIR,
+                       "inode_dir_map", &ctx->inode_dir_map);
        if (pctx.errcode) {
                pctx.num = 2;
                fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
-       pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
-                       _("regular file inode map"), &ctx->inode_reg_map);
+       pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
+                       _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
+                       "inode_reg_map", &ctx->inode_reg_map);
        if (pctx.errcode) {
                pctx.num = 6;
                fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
-       pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
-                                             &ctx->block_found_map);
+       pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
+                       _("in-use block map"), EXT2FS_BMAP64_RBTREE,
+                       "block_found_map", &ctx->block_found_map);
        if (pctx.errcode) {
                pctx.num = 1;
                fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
@@ -578,9 +670,14 @@ void e2fsck_pass1(e2fsck_t ctx)
                return;
        }
        e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
-       if (!ctx->inode_link_info)
+       if (!ctx->inode_link_info) {
+               e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
+                                      "inode_link_info", &save_type);
                pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
                                                     &ctx->inode_link_info);
+               fs->default_bitmap_type = save_type;
+       }
+
        if (pctx.errcode) {
                fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
@@ -620,11 +717,20 @@ void e2fsck_pass1(e2fsck_t ctx)
        }
 
        mark_table_blocks(ctx);
+       pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
+                                               &ctx->block_found_map);
+       if (pctx.errcode) {
+               fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
+               ctx->flags |= E2F_FLAG_ABORT;
+               ext2fs_free_mem(&inode);
+               return;
+       }
        block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
                                                    "block interate buffer");
-       e2fsck_use_inode_shortcuts(ctx, 1);
+       if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
+               e2fsck_use_inode_shortcuts(ctx, 1);
        old_op = ehandler_operation(_("opening inode scan"));
-       pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks, 
+       pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
                                              &scan);
        ehandler_operation(old_op);
        if (pctx.errcode) {
@@ -646,9 +752,19 @@ void e2fsck_pass1(e2fsck_t ctx)
            (fs->super->s_mtime < fs->super->s_inodes_count))
                busted_fs_time = 1;
 
+       if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
+           !(fs->super->s_mmp_block <= fs->super->s_first_data_block ||
+             fs->super->s_mmp_block >= fs->super->s_blocks_count))
+               ext2fs_mark_block_bitmap2(ctx->block_found_map,
+                                         fs->super->s_mmp_block);
+
        while (1) {
+               if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
+                       if (e2fsck_mmp_update(fs))
+                               fatal_error(ctx, 0);
+               }
                old_op = ehandler_operation(_("getting next inode from scan"));
-               pctx.errcode = ext2fs_get_next_inode_full(scan, &ino, 
+               pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
                                                          inode, inode_size);
                ehandler_operation(old_op);
                if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
@@ -656,11 +772,12 @@ void e2fsck_pass1(e2fsck_t ctx)
                if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
                        if (!ctx->inode_bb_map)
                                alloc_bb_map(ctx);
-                       ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
-                       ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
+                       ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
+                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
                        continue;
                }
-               if (pctx.errcode) {
+               if (pctx.errcode &&
+                   pctx.errcode != EXT2_ET_INODE_CSUM_INVALID) {
                        fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
                        ctx->flags |= E2F_FLAG_ABORT;
                        return;
@@ -670,8 +787,16 @@ void e2fsck_pass1(e2fsck_t ctx)
                pctx.ino = ino;
                pctx.inode = inode;
                ctx->stashed_ino = ino;
+
+               /* Clear corrupt inode? */
+               if (pctx.errcode == EXT2_ET_INODE_CSUM_INVALID) {
+                       if (fix_problem(ctx, PR_1_INODE_CSUM_INVALID, &pctx))
+                               goto clear_inode;
+                       failed_csum = 1;
+               }
+
                if (inode->i_links_count) {
-                       pctx.errcode = ext2fs_icount_store(ctx->inode_link_info, 
+                       pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
                                           ino, inode->i_links_count);
                        if (pctx.errcode) {
                                pctx.num = inode->i_links_count;
@@ -696,10 +821,10 @@ void e2fsck_pass1(e2fsck_t ctx)
                 * extent_header_verify is ok.  If the inode is cleared,
                 * no need to swap... so no extra swapping here.
                 */
-               if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs && 
+               if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
                    (inode->i_links_count || (ino == EXT2_BAD_INO) ||
                     (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
-                       if ((ext2fs_extent_header_verify(inode->i_block, 
+                       if ((ext2fs_extent_header_verify(inode->i_block,
                                                 sizeof(inode->i_block)) == 0) &&
                            fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
                                sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
@@ -709,7 +834,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                        clear_inode:
                                e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
                                if (ino == EXT2_BAD_INO)
-                                       ext2fs_mark_inode_bitmap(ctx->inode_used_map, 
+                                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
                                                                 ino);
                                continue;
                        }
@@ -738,12 +863,12 @@ void e2fsck_pass1(e2fsck_t ctx)
 #else
                        ehp = inode->i_block;
 #endif
-                       if ((ext2fs_extent_header_verify(ehp, 
+                       if ((ext2fs_extent_header_verify(ehp,
                                         sizeof(inode->i_block)) == 0) &&
                            (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
                                inode->i_flags |= EXT4_EXTENTS_FL;
 #ifdef WORDS_BIGENDIAN
-                               memcpy(inode->i_block, tmp_block, 
+                               memcpy(inode->i_block, tmp_block,
                                       sizeof(inode->i_block));
 #endif
                                e2fsck_write_inode(ctx, ino, inode, "pass1");
@@ -752,7 +877,15 @@ void e2fsck_pass1(e2fsck_t ctx)
 
                if (ino == EXT2_BAD_INO) {
                        struct process_block_struct pb;
-                       
+
+                       if ((inode->i_mode || inode->i_uid || inode->i_gid ||
+                            inode->i_links_count || inode->i_file_acl) &&
+                           fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
+                               memset(inode, 0, sizeof(struct ext2_inode));
+                               e2fsck_write_inode(ctx, ino, inode,
+                                                  "clear bad inode");
+                       }
+
                        pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
                                                          &pb.fs_meta_blocks);
                        if (pctx.errcode) {
@@ -763,13 +896,14 @@ void e2fsck_pass1(e2fsck_t ctx)
                        }
                        pb.ino = EXT2_BAD_INO;
                        pb.num_blocks = pb.last_block = 0;
+                       pb.last_db_block = -1;
                        pb.num_illegal_blocks = 0;
                        pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
                        pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
                        pb.inode = inode;
                        pb.pctx = &pctx;
                        pb.ctx = ctx;
-                       pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, 
+                       pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
                                     block_buf, process_bad_block, &pb);
                        ext2fs_free_block_bitmap(pb.fs_meta_blocks);
                        if (pctx.errcode) {
@@ -782,7 +916,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                                ctx->flags |= E2F_FLAG_ABORT;
                                return;
                        }
-                       ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
+                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
                        clear_problem_context(&pctx);
                        continue;
                } else if (ino == EXT2_ROOT_INO) {
@@ -812,7 +946,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                                }
                        }
                } else if (ino == EXT2_JOURNAL_INO) {
-                       ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
+                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
                        if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
                                if (!LINUX_S_ISREG(inode->i_mode) &&
                                    fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
@@ -824,20 +958,47 @@ void e2fsck_pass1(e2fsck_t ctx)
                                check_blocks(ctx, &pctx, block_buf);
                                continue;
                        }
-                       if ((inode->i_links_count || inode->i_blocks ||
+                       if ((inode->i_links_count ||
+                            inode->i_blocks || inode->i_block[0]) &&
+                           fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
+                                       &pctx)) {
+                               memset(inode, 0, inode_size);
+                               ext2fs_icount_store(ctx->inode_link_info,
+                                                   ino, 0);
+                               e2fsck_write_inode_full(ctx, ino, inode,
+                                                       inode_size, "pass1");
+                       }
+               } else if ((ino == EXT4_USR_QUOTA_INO) ||
+                          (ino == EXT4_GRP_QUOTA_INO)) {
+                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
+                       if ((fs->super->s_feature_ro_compat &
+                                       EXT4_FEATURE_RO_COMPAT_QUOTA) &&
+                           ((fs->super->s_usr_quota_inum == ino) ||
+                            (fs->super->s_grp_quota_inum == ino))) {
+                               if (!LINUX_S_ISREG(inode->i_mode) &&
+                                   fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
+                                                       &pctx)) {
+                                       inode->i_mode = LINUX_S_IFREG;
+                                       e2fsck_write_inode(ctx, ino, inode,
+                                                       "pass1");
+                               }
+                               check_blocks(ctx, &pctx, block_buf);
+                               continue;
+                       }
+                       if ((inode->i_links_count ||
                             inode->i_blocks || inode->i_block[0]) &&
-                           fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR, 
+                           fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
                                        &pctx)) {
                                memset(inode, 0, inode_size);
                                ext2fs_icount_store(ctx->inode_link_info,
                                                    ino, 0);
-                               e2fsck_write_inode_full(ctx, ino, inode, 
+                               e2fsck_write_inode_full(ctx, ino, inode,
                                                        inode_size, "pass1");
                        }
                } else if (ino < EXT2_FIRST_INODE(fs->super)) {
                        int     problem = 0;
-                       
-                       ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
+
+                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
                        if (ino == EXT2_BOOT_LOADER_INO) {
                                if (LINUX_S_ISDIR(inode->i_mode))
                                        problem = PR_1_RESERVED_BAD_MODE;
@@ -859,12 +1020,13 @@ void e2fsck_pass1(e2fsck_t ctx)
                        check_blocks(ctx, &pctx, block_buf);
                        continue;
                }
+
                /*
                 * Check for inodes who might have been part of the
                 * orphaned list linked list.  They should have gotten
                 * dealt with by now, unless the list had somehow been
                 * corrupted.
-                * 
+                *
                 * FIXME: In the future, inodes which are still in use
                 * (and which are therefore) pending truncation should
                 * be handled specially.  Right now we just clear the
@@ -885,10 +1047,10 @@ void e2fsck_pass1(e2fsck_t ctx)
                                                   "pass1");
                        }
                }
-               
+
                /*
                 * This code assumes that deleted inodes have
-                * i_links_count set to 0.  
+                * i_links_count set to 0.
                 */
                if (!inode->i_links_count) {
                        if (!inode->i_dtime && inode->i_mode) {
@@ -909,7 +1071,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                 * we now assume that the case of non-zero
                 * i_links_count and non-zero dtime means that we
                 * should keep the file, not delete it.
-                * 
+                *
                 */
                if (inode->i_dtime) {
                        if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
@@ -917,8 +1079,8 @@ void e2fsck_pass1(e2fsck_t ctx)
                                e2fsck_write_inode(ctx, ino, inode, "pass1");
                        }
                }
-               
-               ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
+
+               ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
                switch (fs->super->s_creator_os) {
                    case EXT2_OS_HURD:
                        frag = inode->osd2.hurd2.h_i_frag;
@@ -927,12 +1089,16 @@ void e2fsck_pass1(e2fsck_t ctx)
                    default:
                        frag = fsize = 0;
                }
-               
+
                if (inode->i_faddr || frag || fsize ||
                    (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
                        mark_inode_bad(ctx, ino);
+               if (!(fs->super->s_feature_incompat & 
+                     EXT4_FEATURE_INCOMPAT_64BIT) &&
+                   inode->osd2.linux2.l_i_file_acl_high != 0)
+                       mark_inode_bad(ctx, ino);
                if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
-                   !(fs->super->s_feature_ro_compat & 
+                   !(fs->super->s_feature_ro_compat &
                      EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
                    (inode->osd2.linux2.l_i_blocks_hi != 0))
                        mark_inode_bad(ctx, ino);
@@ -940,7 +1106,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                        if (imagic_fs) {
                                if (!ctx->inode_imagic_map)
                                        alloc_imagic_map(ctx);
-                               ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
+                               ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
                                                         ino);
                        } else {
                                if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
@@ -955,23 +1121,23 @@ void e2fsck_pass1(e2fsck_t ctx)
                check_is_really_dir(ctx, &pctx, block_buf);
 
                /*
-                * ext2fs_inode_has_valid_blocks does not actually look
+                * ext2fs_inode_has_valid_blocks2 does not actually look
                 * at i_block[] values, so not endian-sensitive here.
                 */
                if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
                    LINUX_S_ISLNK(inode->i_mode) &&
-                   !ext2fs_inode_has_valid_blocks(inode) &&
+                   !ext2fs_inode_has_valid_blocks2(fs, inode) &&
                    fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
                        inode->i_flags &= ~EXT4_EXTENTS_FL;
                        e2fsck_write_inode(ctx, ino, inode, "pass1");
                }
 
                if (LINUX_S_ISDIR(inode->i_mode)) {
-                       ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
+                       ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
                        e2fsck_add_dir_info(ctx, ino, 0);
                        ctx->fs_directory_count++;
                } else if (LINUX_S_ISREG (inode->i_mode)) {
-                       ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
+                       ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
                        ctx->fs_regular_count++;
                } else if (LINUX_S_ISCHR (inode->i_mode) &&
                           e2fsck_pass1_check_device_inode(fs, inode)) {
@@ -984,7 +1150,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                        check_size(ctx, &pctx);
                        ctx->fs_blockdev_count++;
                } else if (LINUX_S_ISLNK (inode->i_mode) &&
-                          e2fsck_pass1_check_symlink(fs, ino, inode, 
+                          e2fsck_pass1_check_symlink(fs, ino, inode,
                                                      block_buf)) {
                        check_immutable(ctx, &pctx);
                        ctx->fs_symlinks_count++;
@@ -1006,23 +1172,39 @@ void e2fsck_pass1(e2fsck_t ctx)
                        ctx->fs_sockets_count++;
                } else
                        mark_inode_bad(ctx, ino);
-               if (inode->i_block[EXT2_IND_BLOCK])
-                       ctx->fs_ind_count++;
-               if (inode->i_block[EXT2_DIND_BLOCK])
-                       ctx->fs_dind_count++;
-               if (inode->i_block[EXT2_TIND_BLOCK])
-                       ctx->fs_tind_count++;
+               if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
+                       if (inode->i_block[EXT2_IND_BLOCK])
+                               ctx->fs_ind_count++;
+                       if (inode->i_block[EXT2_DIND_BLOCK])
+                               ctx->fs_dind_count++;
+                       if (inode->i_block[EXT2_TIND_BLOCK])
+                               ctx->fs_tind_count++;
+               }
                if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
                    (inode->i_block[EXT2_IND_BLOCK] ||
                     inode->i_block[EXT2_DIND_BLOCK] ||
                     inode->i_block[EXT2_TIND_BLOCK] ||
-                    inode->i_file_acl)) {
+                    ext2fs_file_acl_block(fs, inode))) {
                        inodes_to_process[process_inode_count].ino = ino;
                        inodes_to_process[process_inode_count].inode = *inode;
                        process_inode_count++;
                } else
                        check_blocks(ctx, &pctx, block_buf);
 
+               /*
+                * If the inode failed the checksum and the user didn't
+                * clear the inode, test the checksum again -- if it still
+                * fails, ask the user if the checksum should be corrected.
+                */
+               if (failed_csum) {
+                       pctx.errcode = recheck_bad_inode_checksum(fs, ino, ctx,
+                                                                 &pctx);
+                       if (pctx.errcode) {
+                               ctx->flags |= E2F_FLAG_ABORT;
+                               return;
+                       }
+               }
+
                if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
                        return;
 
@@ -1052,7 +1234,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                ea_refcount_free(ctx->refcount_extra);
                ctx->refcount_extra = 0;
        }
-               
+
        if (ctx->invalid_bitmaps)
                handle_fs_bad_blocks(ctx);
 
@@ -1070,20 +1252,24 @@ void e2fsck_pass1(e2fsck_t ctx)
                clear_problem_context(&pctx);
                pctx.errcode = ext2fs_create_resize_inode(fs);
                if (pctx.errcode) {
-                       fix_problem(ctx, PR_1_RESIZE_INODE_CREATE, &pctx);
-                       /* Should never get here */
-                       ctx->flags |= E2F_FLAG_ABORT;
-                       return;
+                       if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
+                                        &pctx)) {
+                               ctx->flags |= E2F_FLAG_ABORT;
+                               return;
+                       }
+                       pctx.errcode = 0;
+               }
+               if (!pctx.errcode) {
+                       e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
+                                         "recreate inode");
+                       inode->i_mtime = ctx->now;
+                       e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
+                                          "recreate inode");
                }
-               e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
-                                 "recreate inode");
-               inode->i_mtime = ctx->now;
-               e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode, 
-                                  "recreate inode");
                fs->block_map = save_bmap;
                ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
        }
-                      
+
        if (ctx->flags & E2F_FLAG_RESTART) {
                /*
                 * Only the master copy of the superblock and block
@@ -1106,23 +1292,18 @@ void e2fsck_pass1(e2fsck_t ctx)
        ext2fs_free_mem(&inodes_to_process);
 endit:
        e2fsck_use_inode_shortcuts(ctx, 0);
-       
+
        ext2fs_free_mem(&block_buf);
        ext2fs_free_mem(&inode);
 
-#ifdef RESOURCE_TRACK
-       if (ctx->options & E2F_OPT_TIME2) {
-               e2fsck_clear_progbar(ctx);
-               print_resource_track(_("Pass 1"), &rtrack, ctx->fs->io);
-       }
-#endif
+       print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
 }
 
 /*
  * When the inode_scan routines call this callback at the end of the
  * glock group, call process_inodes.
  */
-static errcode_t scan_callback(ext2_filsys fs, 
+static errcode_t scan_callback(ext2_filsys fs,
                               ext2_inode_scan scan EXT2FS_ATTR((unused)),
                               dgrp_t group, void * priv_data)
 {
@@ -1131,7 +1312,7 @@ static errcode_t scan_callback(ext2_filsys fs,
 
        scan_struct = (struct scan_callback_struct *) priv_data;
        ctx = scan_struct->ctx;
-       
+
        process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
 
        if (ctx->progress)
@@ -1153,7 +1334,7 @@ static void process_inodes(e2fsck_t ctx, char *block_buf)
        const char              *old_operation;
        char                    buf[80];
        struct problem_context  pctx;
-       
+
 #if 0
        printf("begin process_inodes: ");
 #endif
@@ -1168,7 +1349,7 @@ static void process_inodes(e2fsck_t ctx, char *block_buf)
        for (i=0; i < process_inode_count; i++) {
                pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
                pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
-               
+
 #if 0
                printf("%u ", pctx.ino);
 #endif
@@ -1195,11 +1376,19 @@ static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
        const struct process_inode_block *ib_b =
                (const struct process_inode_block *) b;
        int     ret;
-       
+
        ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
               ib_b->inode.i_block[EXT2_IND_BLOCK]);
        if (ret == 0)
-               ret = ib_a->inode.i_file_acl - ib_b->inode.i_file_acl;
+               /*
+                * We only call process_inodes() for non-extent
+                * inodes, so it's OK to pass NULL to
+                * ext2fs_file_acl_block() here.
+                */
+               ret = ext2fs_file_acl_block(0, &(ib_a->inode)) -
+                       ext2fs_file_acl_block(0, &(ib_b->inode));
+       if (ret == 0)
+               ret = ib_a->ino - ib_b->ino;
        return ret;
 }
 
@@ -1212,9 +1401,10 @@ static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
 
        if (!ctx->inode_bad_map) {
                clear_problem_context(&pctx);
-       
-               pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
-                           _("bad inode map"), &ctx->inode_bad_map);
+
+               pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
+                               _("bad inode map"), EXT2FS_BMAP64_RBTREE,
+                               "inode_bad_map", &ctx->inode_bad_map);
                if (pctx.errcode) {
                        pctx.num = 3;
                        fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
@@ -1223,7 +1413,7 @@ static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
                        return;
                }
        }
-       ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
+       ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
 }
 
 
@@ -1233,11 +1423,11 @@ static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
 static void alloc_bb_map(e2fsck_t ctx)
 {
        struct          problem_context pctx;
-       
+
        clear_problem_context(&pctx);
-       pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
-                                             _("inode in bad block map"),
-                                             &ctx->inode_bb_map);
+       pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
+                       _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
+                       "inode_bb_map", &ctx->inode_bb_map);
        if (pctx.errcode) {
                pctx.num = 4;
                fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
@@ -1253,11 +1443,11 @@ static void alloc_bb_map(e2fsck_t ctx)
 static void alloc_imagic_map(e2fsck_t ctx)
 {
        struct          problem_context pctx;
-       
+
        clear_problem_context(&pctx);
-       pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
-                                             _("imagic inode map"),
-                                             &ctx->inode_imagic_map);
+       pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
+                       _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
+                       "inode_imagic_map", &ctx->inode_imagic_map);
        if (pctx.errcode) {
                pctx.num = 5;
                fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
@@ -1274,29 +1464,30 @@ static void alloc_imagic_map(e2fsck_t ctx)
  * WARNING: Assumes checks have already been done to make sure block
  * is valid.  This is true in both process_block and process_bad_block.
  */
-static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
+static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
 {
        struct          problem_context pctx;
-       
+
        clear_problem_context(&pctx);
-       
-       if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
+
+       if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
                if (!ctx->block_dup_map) {
-                       pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
-                             _("multiply claimed block map"),
-                             &ctx->block_dup_map);
+                       pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
+                                       _("multiply claimed block map"),
+                                       EXT2FS_BMAP64_RBTREE, "block_dup_map",
+                                       &ctx->block_dup_map);
                        if (pctx.errcode) {
                                pctx.num = 3;
-                               fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, 
+                               fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
                                            &pctx);
                                /* Should never get here */
                                ctx->flags |= E2F_FLAG_ABORT;
                                return;
                        }
                }
-               ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
+               ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
        } else {
-               ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
+               ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
        }
 }
 
@@ -1307,24 +1498,24 @@ static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
  * EA blocks that had extra references as accounted for in
  * ctx->refcount_extra.
  */
-static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount, 
+static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
                                    char *block_buf, int adjust_sign)
 {
        struct ext2_ext_attr_header     *header;
        struct problem_context          pctx;
        ext2_filsys                     fs = ctx->fs;
-       blk_t                           blk;
+       blk64_t                         blk;
        __u32                           should_be;
        int                             count;
 
        clear_problem_context(&pctx);
-       
+
        ea_refcount_intr_begin(refcount);
        while (1) {
                if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
                        break;
                pctx.blk = blk;
-               pctx.errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
+               pctx.errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
                if (pctx.errcode) {
                        fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
                        return;
@@ -1335,10 +1526,11 @@ static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
                pctx.num = should_be;
                if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
                        header->h_refcount = should_be;
-                       pctx.errcode = ext2fs_write_ext_attr(fs, blk,
+                       pctx.errcode = ext2fs_write_ext_attr2(fs, blk,
                                                             block_buf);
                        if (pctx.errcode) {
-                               fix_problem(ctx, PR_1_EXTATTR_WRITE, &pctx);
+                               fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
+                                           &pctx);
                                continue;
                        }
                }
@@ -1354,14 +1546,14 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
        ext2_filsys fs = ctx->fs;
        ext2_ino_t      ino = pctx->ino;
        struct ext2_inode *inode = pctx->inode;
-       blk_t           blk;
+       blk64_t         blk;
        char *          end;
        struct ext2_ext_attr_header *header;
        struct ext2_ext_attr_entry *entry;
        int             count;
        region_t        region = 0;
 
-       blk = inode->i_file_acl;
+       blk = ext2fs_file_acl_block(fs, inode);
        if (blk == 0)
                return 0;
 
@@ -1374,16 +1566,17 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
         */
        if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
            (blk < fs->super->s_first_data_block) ||
-           (blk >= fs->super->s_blocks_count)) {
+           (blk >= ext2fs_blocks_count(fs->super))) {
                mark_inode_bad(ctx, ino);
                return 0;
        }
 
        /* If ea bitmap hasn't been allocated, create it */
        if (!ctx->block_ea_map) {
-               pctx->errcode = ext2fs_allocate_block_bitmap(fs,
-                                                     _("ext attr block map"),
-                                                     &ctx->block_ea_map);
+               pctx->errcode = e2fsck_allocate_block_bitmap(fs,
+                                       _("ext attr block map"),
+                                       EXT2FS_BMAP64_RBTREE, "block_ea_map",
+                                       &ctx->block_ea_map);
                if (pctx->errcode) {
                        pctx->num = 2;
                        fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
@@ -1409,7 +1602,7 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
 #endif
 
        /* Have we seen this EA block before? */
-       if (ext2fs_fast_test_block_bitmap(ctx->block_ea_map, blk)) {
+       if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
                if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
                        return 1;
                /* Ooops, this EA was referenced more than it stated */
@@ -1432,11 +1625,11 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
         * validate it
         */
        pctx->blk = blk;
-       pctx->errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
+       pctx->errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
        if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
                goto clear_extattr;
        header = (struct ext2_ext_attr_header *) block_buf;
-       pctx->blk = inode->i_file_acl;
+       pctx->blk = ext2fs_file_acl_block(fs, inode);
        if (((ctx->ext_attr_ver == 1) &&
             (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
            ((ctx->ext_attr_ver == 2) &&
@@ -1452,7 +1645,7 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
 
        region = region_create(0, fs->blocksize);
        if (!region) {
-               fix_problem(ctx, PR_1_EA_ALLOC_REGION, pctx);
+               fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return 0;
        }
@@ -1518,13 +1711,13 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
        if (count)
                ea_refcount_store(ctx->refcount, blk, count);
        mark_block_used(ctx, blk);
-       ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk);
+       ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
        return 1;
 
 clear_extattr:
        if (region)
                region_free(region);
-       inode->i_file_acl = 0;
+       ext2fs_file_acl_block_set(fs, inode, 0);
        e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
        return 0;
 }
@@ -1537,7 +1730,7 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
        struct ext2_dx_root_info        *root;
        ext2_filsys                     fs = ctx->fs;
        errcode_t                       retval;
-       blk_t                           blk;
+       blk64_t                         blk;
 
        if ((!LINUX_S_ISDIR(inode->i_mode) &&
             fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
@@ -1545,22 +1738,22 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
             fix_problem(ctx, PR_1_HTREE_SET, pctx)))
                return 1;
 
-       pctx->errcode = ext2fs_bmap(fs, ino, inode, 0, 0, 0, &blk);
+       pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
 
        if ((pctx->errcode) ||
            (blk == 0) ||
            (blk < fs->super->s_first_data_block) ||
-           (blk >= fs->super->s_blocks_count)) {
+           (blk >= ext2fs_blocks_count(fs->super))) {
                if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
                        return 1;
                else
                        return 0;
        }
 
-       retval = io_channel_read_blk(fs->io, blk, 1, block_buf);
+       retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
        if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
                return 1;
-       
+
        /* XXX should check that beginning matches a directory */
        root = (struct ext2_dx_root_info *) (block_buf + 24);
 
@@ -1574,7 +1767,7 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
            (root->hash_version != EXT2_HASH_TEA) &&
            fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
                return 1;
-               
+
        if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
            fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
                return 1;
@@ -1583,7 +1776,7 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
        if ((root->indirect_levels > 1) &&
            fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
                return 1;
-       
+
        return 0;
 }
 
@@ -1596,12 +1789,12 @@ void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
        ext2fs_icount_store(ctx->inode_link_info, ino, 0);
        inode->i_dtime = ctx->now;
 
-       ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
-       ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
+       ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
+       ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
        if (ctx->inode_reg_map)
-               ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
+               ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
        if (ctx->inode_bad_map)
-               ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
+               ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
 
        /*
         * If the inode was partially accounted for before processing
@@ -1609,6 +1802,9 @@ void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
         */
        ctx->flags |= restart_flag;
 
+       if (ino == EXT2_BAD_INO)
+               memset(inode, 0, sizeof(struct ext2_inode));
+
        e2fsck_write_inode(ctx, ino, inode, source);
 }
 
@@ -1618,9 +1814,9 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                             ext2_extent_handle_t ehandle)
 {
        struct ext2fs_extent    extent;
-       blk_t                   blk;
+       blk64_t                 blk;
        e2_blkcnt_t             blockcnt;
-       int                     i;
+       unsigned int            i;
        int                     is_dir, is_leaf;
        errcode_t               problem;
        struct ext2_extent_info info;
@@ -1636,29 +1832,30 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
 
                problem = 0;
-               if (extent.e_pblk < ctx->fs->super->s_first_data_block ||
-                   extent.e_pblk >= ctx->fs->super->s_blocks_count)
+               if (extent.e_pblk == 0 ||
+                   extent.e_pblk < ctx->fs->super->s_first_data_block ||
+                   extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
                        problem = PR_1_EXTENT_BAD_START_BLK;
                else if (extent.e_lblk < start_block)
                        problem = PR_1_OUT_OF_ORDER_EXTENTS;
+               else if (is_leaf && extent.e_len == 0)
+                       problem = PR_1_EXTENT_LENGTH_ZERO;
                else if (is_leaf &&
                         (extent.e_pblk + extent.e_len) >
-                        ctx->fs->super->s_blocks_count)
+                        ext2fs_blocks_count(ctx->fs->super))
                        problem = PR_1_EXTENT_ENDS_BEYOND;
 
                if (problem) {
+               report_problem:
                        pctx->blk = extent.e_pblk;
                        pctx->blk2 = extent.e_lblk;
                        pctx->num = extent.e_len;
                        if (fix_problem(ctx, problem, pctx)) {
+                               e2fsck_read_bitmaps(ctx);
                                pctx->errcode =
                                        ext2fs_extent_delete(ehandle, 0);
                                if (pctx->errcode) {
-                                       fix_problem(ctx,
-                                                   PR_1_EXTENT_DELETE_FAIL,
-                                                   pctx);
-                                       /* Should never get here */
-                                       ctx->flags |= E2F_FLAG_ABORT;
+                                       pctx->str = "ext2fs_extent_delete";
                                        return;
                                }
                                pctx->errcode = ext2fs_extent_get(ehandle,
@@ -1674,36 +1871,82 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                }
 
                if (!is_leaf) {
-                       mark_block_used(ctx, extent.e_pblk);
-                       pb->num_blocks++;
+                       blk = extent.e_pblk;
                        pctx->errcode = ext2fs_extent_get(ehandle,
                                                  EXT2_EXTENT_DOWN, &extent);
                        if (pctx->errcode) {
-                               printf("Error1: %s on inode %lld\n",
-                                       error_message(pctx->errcode), pctx->ino);
-                               abort();
+                               pctx->str = "EXT2_EXTENT_DOWN";
+                               problem = PR_1_EXTENT_HEADER_INVALID;
+                               if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
+                                       goto report_problem;
+                               return;
                        }
                        scan_extent_node(ctx, pctx, pb, extent.e_lblk, ehandle);
+                       if (pctx->errcode)
+                               return;
                        pctx->errcode = ext2fs_extent_get(ehandle,
                                                  EXT2_EXTENT_UP, &extent);
                        if (pctx->errcode) {
-                               printf("Error1: %s on inode %lld\n",
-                                       error_message(pctx->errcode), pctx->ino);
-                               abort();
+                               pctx->str = "EXT2_EXTENT_UP";
+                               return;
                        }
+                       mark_block_used(ctx, blk);
+                       pb->num_blocks++;
                        goto next;
                }
 
+               if ((pb->previous_block != 0) &&
+                   (pb->previous_block+1 != extent.e_pblk)) {
+                       if (ctx->options & E2F_OPT_FRAGCHECK) {
+                               char type = '?';
+
+                               if (pb->is_dir)
+                                       type = 'd';
+                               else if (pb->is_reg)
+                                       type = 'f';
+
+                               printf(("%6lu(%c): expecting %6lu "
+                                       "actual extent "
+                                       "phys %6lu log %lu len %lu\n"),
+                                      (unsigned long) pctx->ino, type,
+                                      (unsigned long) pb->previous_block+1,
+                                      (unsigned long) extent.e_pblk,
+                                      (unsigned long) extent.e_lblk,
+                                      (unsigned long) extent.e_len);
+                       }
+                       pb->fragmented = 1;
+               }
+               while (is_dir && ++pb->last_db_block < extent.e_lblk) {
+                       pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
+                                                             pb->ino, 0,
+                                                             pb->last_db_block);
+                       if (pctx->errcode) {
+                               pctx->blk = 0;
+                               pctx->num = pb->last_db_block;
+                               goto failed_add_dir_block;
+                       }
+               }
                for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
                     i < extent.e_len;
                     blk++, blockcnt++, i++) {
-                       mark_block_used(ctx, blk);
+                       if (!(ctx->fs->cluster_ratio_bits &&
+                             pb->previous_block &&
+                             (EXT2FS_B2C(ctx->fs, blk) ==
+                              EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
+                             (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
+                             (blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
+                               mark_block_used(ctx, blk);
+                               pb->num_blocks++;
+                       }
+
+                       pb->previous_block = blk;
 
                        if (is_dir) {
-                               pctx->errcode = ext2fs_add_dir_block(ctx->fs->dblist, pctx->ino, blk, blockcnt);
+                               pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
                                if (pctx->errcode) {
                                        pctx->blk = blk;
                                        pctx->num = blockcnt;
+                               failed_add_dir_block:
                                        fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
                                        /* Should never get here */
                                        ctx->flags |= E2F_FLAG_ABORT;
@@ -1711,8 +1954,13 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                                }
                        }
                }
-               pb->num_blocks += extent.e_len;
+               if (is_dir && extent.e_len > 0)
+                       pb->last_db_block = blockcnt - 1;
+               pb->previous_block = extent.e_pblk + extent.e_len - 1;
                start_block = pb->last_block = extent.e_lblk + extent.e_len - 1;
+               if (is_leaf && !is_dir &&
+                   !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
+                       pb->last_init_lblock = extent.e_lblk + extent.e_len - 1;
        next:
                pctx->errcode = ext2fs_extent_get(ehandle,
                                                  EXT2_EXTENT_NEXT_SIB,
@@ -1723,24 +1971,40 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
 }
 
 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
-                                struct process_block_struct *pb, 
-                                char *block_buf)
+                                struct process_block_struct *pb)
 {
+       struct ext2_extent_info info;
        struct ext2_inode       *inode = pctx->inode;
        ext2_extent_handle_t    ehandle;
        ext2_filsys             fs = ctx->fs;
        ext2_ino_t              ino = pctx->ino;
+       errcode_t               retval;
 
-       pctx->errcode = ext2fs_extent_open(fs, ino, &ehandle);
-       if (pctx->errcode &&
-           fix_problem(ctx, PR_1_READ_EXTENT, pctx)) {
-               e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks_extents");
+       pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
+       if (pctx->errcode) {
+               if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
+                       e2fsck_clear_inode(ctx, ino, inode, 0,
+                                          "check_blocks_extents");
                pctx->errcode = 0;
                return;
        }
 
-       scan_extent_node(ctx, pctx, pb, 0, ehandle);
+       retval = ext2fs_extent_get_info(ehandle, &info);
+       if (retval == 0) {
+               if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
+                       info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
+               ctx->extent_depth_count[info.max_depth]++;
+       }
 
+       scan_extent_node(ctx, pctx, pb, 0, ehandle);
+       if (pctx->errcode &&
+           fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
+               pb->num_blocks = 0;
+               inode->i_blocks = 0;
+               e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
+                                  "check_blocks_extents");
+               pctx->errcode = 0;
+       }
        ext2fs_extent_free(ehandle);
 }
 
@@ -1757,11 +2021,14 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        struct ext2_inode *inode = pctx->inode;
        int             bad_size = 0;
        int             dirty_inode = 0;
+       int             extent_fs;
        __u64           size;
-       
+
        pb.ino = ino;
        pb.num_blocks = 0;
        pb.last_block = -1;
+       pb.last_init_lblock = -1;
+       pb.last_db_block = -1;
        pb.num_illegal_blocks = 0;
        pb.suppress = 0; pb.clear = 0;
        pb.fragmented = 0;
@@ -1776,6 +2043,9 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        pctx->ino = ino;
        pctx->errcode = 0;
 
+       extent_fs = (ctx->fs->super->s_feature_incompat &
+                     EXT3_FEATURE_INCOMPAT_EXTENTS);
+
        if (inode->i_flags & EXT2_COMPRBLK_FL) {
                if (fs->super->s_feature_incompat &
                    EXT2_FEATURE_INCOMPAT_COMPRESSION)
@@ -1788,21 +2058,26 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                }
        }
 
-       if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf)) {
+       if (ext2fs_file_acl_block(fs, inode) &&
+           check_ext_attr(ctx, pctx, block_buf)) {
                if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
                        goto out;
                pb.num_blocks++;
        }
 
-       if (ext2fs_inode_has_valid_blocks(inode)) {
-               if ((ctx->fs->super->s_feature_incompat &
-                    EXT3_FEATURE_INCOMPAT_EXTENTS) &&
-                   (inode->i_flags & EXT4_EXTENTS_FL))
-                       check_blocks_extents(ctx, pctx, &pb, block_buf);
-               else
-                       pctx->errcode = ext2fs_block_iterate2(fs, ino,
+       if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
+               if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
+                       check_blocks_extents(ctx, pctx, &pb);
+               else {
+                       pctx->errcode = ext2fs_block_iterate3(fs, ino,
                                                pb.is_dir ? BLOCK_FLAG_HOLE : 0,
                                                block_buf, process_block, &pb);
+                       /*
+                        * We do not have uninitialized extents in non extent
+                        * files.
+                        */
+                       pb.last_init_lblock = pb.last_block;
+               }
        }
        end_problem_latch(ctx, PR_LATCH_BLOCK);
        end_problem_latch(ctx, PR_LATCH_TOOBIG);
@@ -1811,34 +2086,18 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        if (pctx->errcode)
                fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
 
-       if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
-               ctx->fs_fragmented++;
+       if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
+               if (LINUX_S_ISDIR(inode->i_mode))
+                       ctx->fs_fragmented_dir++;
+               else
+                       ctx->fs_fragmented++;
+       }
 
        if (pb.clear) {
                e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
                                   "check_blocks");
                return;
        }
-       
-       if (pb.is_dir) {
-               while (1) {
-                       struct ext2_db_entry *entry;
-
-                       if (ext2fs_dblist_get_last(fs->dblist, &entry) ||
-                           (entry->ino != ino) ||
-                           (entry->blk != 0) ||
-                           (entry->blockcnt == 0))
-                               break;
-                       /* printf("Dropping ino %lu blk %lu blockcnt %d\n", 
-                                 entry->ino, entry->blk, entry->blockcnt); */
-                       ext2fs_dblist_drop_last(fs->dblist);
-                       if (ext2fs_dblist_get_last(fs->dblist, &entry) ||
-                           (entry->ino != ino))
-                               pb.last_block--;
-                       else
-                               pb.last_block = entry->blockcnt;
-               }
-       }
 
        if (inode->i_flags & EXT2_INDEX_FL) {
                if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
@@ -1850,11 +2109,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
 #endif
                }
        }
-       if (ctx->dirs_to_hash && pb.is_dir &&
-           !(inode->i_flags & EXT2_INDEX_FL) &&
-           ((inode->i_size / fs->blocksize) >= 3))
-               ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
-               
+
        if (!pb.num_blocks && pb.is_dir) {
                if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
                        e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
@@ -1863,18 +2118,25 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                }
        }
 
+       if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
+               quota_data_add(ctx->qctx, inode, ino,
+                              pb.num_blocks * fs->blocksize);
+               quota_data_inodes(ctx->qctx, inode, ino, +1);
+       }
+
        if (!(fs->super->s_feature_ro_compat &
              EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
            !(inode->i_flags & EXT4_HUGE_FILE_FL))
                pb.num_blocks *= (fs->blocksize / 512);
+       pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
 #if 0
-       printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
-              ino, inode->i_size, pb.last_block, inode->i_blocks,
+       printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
+              ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
               pb.num_blocks);
 #endif
        if (pb.is_dir) {
                int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
-               if (inode->i_size & (fs->blocksize - 1)) 
+               if (inode->i_size & (fs->blocksize - 1))
                        bad_size = 5;
                else if (nblock > (pb.last_block + 1))
                        bad_size = 1;
@@ -1887,18 +2149,27 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                e2_blkcnt_t blkpg = ctx->blocks_per_page;
 
                size = EXT2_I_SIZE(inode);
-               if ((pb.last_block >= 0) &&
+               if ((pb.last_init_lblock >= 0) &&
                    /* allow allocated blocks to end of PAGE_SIZE */
-                   (size < (__u64)pb.last_block * fs->blocksize) &&
-                   (pb.last_block / blkpg * blkpg != pb.last_block ||
-                    size < (__u64)(pb.last_block & ~(blkpg-1)) *fs->blocksize))
+                   (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
+                   (pb.last_init_lblock / blkpg * blkpg != pb.last_init_lblock ||
+                    size < (__u64)(pb.last_init_lblock & ~(blkpg-1)) *
+                    fs->blocksize))
                        bad_size = 3;
-               else if (size > ext2_max_sizes[fs->super->s_log_block_size])
+               else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
+                        size > ext2_max_sizes[fs->super->s_log_block_size])
+                       /* too big for a direct/indirect-mapped file */
                        bad_size = 4;
+               else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
+                        size >
+                        ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
+                       /* too big for an extent-based file - 32bit ee_block */
+                       bad_size = 6;
        }
        /* i_size for symlinks is checked elsewhere */
        if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
                pctx->num = (pb.last_block+1) * fs->blocksize;
+               pctx->group = bad_size;
                if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
                        inode->i_size = pctx->num;
                        if (!LINUX_S_ISDIR(inode->i_mode))
@@ -1907,10 +2178,9 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                }
                pctx->num = 0;
        }
-       if (LINUX_S_ISREG(inode->i_mode) &&
-           (inode->i_size_high || inode->i_size & 0x80000000UL))
+       if (LINUX_S_ISREG(inode->i_mode) && EXT2_I_SIZE(inode) >= 0x80000000UL)
                ctx->large_files++;
-       if ((pb.num_blocks != inode->i_blocks) ||
+       if ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
            ((fs->super->s_feature_ro_compat &
              EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
             (inode->i_flags & EXT4_HUGE_FILE_FL) &&
@@ -1918,11 +2188,17 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                pctx->num = pb.num_blocks;
                if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
                        inode->i_blocks = pb.num_blocks;
-                       inode->osd2.linux2.l_i_blocks_hi = 0;
+                       inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
                        dirty_inode++;
                }
                pctx->num = 0;
        }
+
+       if (ctx->dirs_to_hash && pb.is_dir &&
+           !(inode->i_flags & EXT2_INDEX_FL) &&
+           ((inode->i_size / fs->blocksize) >= 3))
+               ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
+
 out:
        if (dirty_inode)
                e2fsck_write_inode(ctx, ino, inode, "check_blocks");
@@ -1933,9 +2209,9 @@ out:
  * Helper function called by process block when an illegal block is
  * found.  It returns a description about why the block is illegal
  */
-static char *describe_illegal_block(ext2_filsys fs, blk_t block)
+static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
 {
-       blk_t   super;
+       blk64_t super;
        int     i;
        static char     problem[80];
 
@@ -1944,8 +2220,8 @@ static char *describe_illegal_block(ext2_filsys fs, blk_t block)
        if (block < super) {
                sprintf(problem, "< FIRSTBLOCK (%u)", super);
                return(problem);
-       } else if (block >= fs->super->s_blocks_count) {
-               sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
+       } else if (block >= ext2fs_blocks_count(fs->super)) {
+               sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
                return(problem);
        }
        for (i = 0; i < fs->group_desc_count; i++) {
@@ -1959,16 +2235,16 @@ static char *describe_illegal_block(ext2_filsys fs, blk_t block)
                                "of group %d", i);
                        break;
                }
-               if (block == fs->group_desc[i].bg_block_bitmap) {
+               if (block == ext2fs_block_bitmap_loc(fs, i)) {
                        sprintf(problem, "is the block bitmap of group %d", i);
                        break;
                }
-               if (block == fs->group_desc[i].bg_inode_bitmap) {
+               if (block == ext2fs_inode_bitmap_loc(fs, i)) {
                        sprintf(problem, "is the inode bitmap of group %d", i);
                        break;
                }
-               if (block >= fs->group_desc[i].bg_inode_table &&
-                   (block < fs->group_desc[i].bg_inode_table
+               if (block >= ext2fs_inode_table_loc(fs, i) &&
+                   (block < ext2fs_inode_table_loc(fs, i)
                     + fs->inode_blocks_per_group)) {
                        sprintf(problem, "is in the inode table of group %d",
                                i);
@@ -1984,15 +2260,15 @@ static char *describe_illegal_block(ext2_filsys fs, blk_t block)
  * This is a helper function for check_blocks().
  */
 static int process_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;
        struct problem_context *pctx;
-       blk_t   blk = *block_nr;
+       blk64_t blk = *block_nr;
        int     ret_code = 0;
        int     problem = 0;
        e2fsck_t        ctx;
@@ -2023,47 +2299,39 @@ static int process_block(ext2_filsys fs,
                return 0;
        }
 
-       if (blk == 0) {
-               if (p->is_dir == 0) {
-                       /*
-                        * Should never happen, since only directories
-                        * get called with BLOCK_FLAG_HOLE
-                        */
-#if DEBUG_E2FSCK
-                       printf("process_block() called with blk == 0, "
-                              "blockcnt=%d, inode %lu???\n",
-                              blockcnt, p->ino);
-#endif
-                       return 0;
-               }
-               if (blockcnt < 0)
-                       return 0;
-               if (blockcnt * fs->blocksize < p->inode->i_size) {
-#if 0
-                       printf("Missing block (#%d) in directory inode %lu!\n",
-                              blockcnt, p->ino);
-#endif
-                       p->last_block = blockcnt;
-                       goto mark_dir;
-               }
+       if (blk == 0)
                return 0;
-       }
 
 #if 0
        printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
               blockcnt);
 #endif
-       
+
        /*
         * Simplistic fragmentation check.  We merely require that the
         * file be contiguous.  (Which can never be true for really
         * big files that are greater than a block group.)
         */
-       if (!HOLE_BLKADDR(p->previous_block)) {
-               if (p->previous_block+1 != blk)
+       if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
+               if (p->previous_block+1 != blk) {
+                       if (ctx->options & E2F_OPT_FRAGCHECK) {
+                               char type = '?';
+
+                               if (p->is_dir)
+                                       type = 'd';
+                               else if (p->is_reg)
+                                       type = 'f';
+
+                               printf(_("%6lu(%c): expecting %6lu "
+                                        "got phys %6lu (blkcnt %lld)\n"),
+                                      (unsigned long) pctx->ino, type,
+                                      (unsigned long) p->previous_block+1,
+                                      (unsigned long) blk,
+                                      blockcnt);
+                       }
                        p->fragmented = 1;
+               }
        }
-       p->previous_block = blk;
 
        if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
                problem = PR_1_TOOBIG_DIR;
@@ -2071,9 +2339,9 @@ static int process_block(ext2_filsys fs,
                problem = PR_1_TOOBIG_REG;
        if (!p->is_dir && !p->is_reg && blockcnt > 0)
                problem = PR_1_TOOBIG_SYMLINK;
-           
+
        if (blk < fs->super->s_first_data_block ||
-           blk >= fs->super->s_blocks_count)
+           blk >= ext2fs_blocks_count(fs->super))
                problem = PR_1_ILLEGAL_BLOCK_NUM;
 
        if (problem) {
@@ -2100,7 +2368,7 @@ static int process_block(ext2_filsys fs,
        }
 
        if (p->ino == EXT2_RESIZE_INO) {
-               /* 
+               /*
                 * The resize inode has already be sanity checked
                 * during pass #0 (the superblock checks).  All we
                 * have to do is mark the double indirect block as
@@ -2109,18 +2377,37 @@ static int process_block(ext2_filsys fs,
                 */
                if (blockcnt == BLOCK_COUNT_DIND)
                        mark_block_used(ctx, blk);
-       } else
+               p->num_blocks++;
+       } else if (!(ctx->fs->cluster_ratio_bits &&
+                    p->previous_block &&
+                    (EXT2FS_B2C(ctx->fs, blk) ==
+                     EXT2FS_B2C(ctx->fs, p->previous_block)) &&
+                    (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
+                    (blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
                mark_block_used(ctx, blk);
-       p->num_blocks++;
+               p->num_blocks++;
+       }
        if (blockcnt >= 0)
                p->last_block = blockcnt;
+       p->previous_block = blk;
 mark_dir:
        if (p->is_dir && (blockcnt >= 0)) {
-               pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
-                                                   blk, blockcnt);
+               while (++p->last_db_block < blockcnt) {
+                       pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
+                                                             p->ino, 0,
+                                                             p->last_db_block);
+                       if (pctx->errcode) {
+                               pctx->blk = 0;
+                               pctx->num = p->last_db_block;
+                               goto failed_add_dir_block;
+                       }
+               }
+               pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
+                                                     blk, blockcnt);
                if (pctx->errcode) {
                        pctx->blk = blk;
                        pctx->num = blockcnt;
+               failed_add_dir_block:
                        fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
                        /* Should never get here */
                        ctx->flags |= E2F_FLAG_ABORT;
@@ -2131,15 +2418,15 @@ mark_dir:
 }
 
 static int process_bad_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;
-       blk_t           first_block;
+       blk64_t         blk = *block_nr;
+       blk64_t         first_block;
        dgrp_t          i;
        struct problem_context *pctx;
        e2fsck_t        ctx;
@@ -2151,17 +2438,17 @@ static int process_bad_block(ext2_filsys fs,
 
        if (!blk)
                return 0;
-       
+
        p = (struct process_block_struct *) priv_data;
        ctx = p->ctx;
        pctx = p->pctx;
-       
+
        pctx->ino = EXT2_BAD_INO;
        pctx->blk = blk;
        pctx->blkcount = blockcnt;
 
        if ((blk < fs->super->s_first_data_block) ||
-           (blk >= fs->super->s_blocks_count)) {
+           (blk >= ext2fs_blocks_count(fs->super))) {
                if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
                        *block_nr = 0;
                        return BLOCK_CHANGED;
@@ -2170,16 +2457,16 @@ static int process_bad_block(ext2_filsys fs,
        }
 
        if (blockcnt < 0) {
-               if (ext2fs_test_block_bitmap(p->fs_meta_blocks, blk)) {
+               if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
                        p->bbcheck = 1;
                        if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
                                *block_nr = 0;
                                return BLOCK_CHANGED;
                        }
-               } else if (ext2fs_test_block_bitmap(ctx->block_found_map, 
+               } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
                                                    blk)) {
                        p->bbcheck = 1;
-                       if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, 
+                       if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
                                        pctx)) {
                                *block_nr = 0;
                                return BLOCK_CHANGED;
@@ -2190,7 +2477,7 @@ static int process_bad_block(ext2_filsys fs,
                        mark_block_used(ctx, blk);
                return 0;
        }
-#if 0 
+#if 0
        printf ("DEBUG: Marking %u as bad.\n", blk);
 #endif
        ctx->fs_badblocks_count++;
@@ -2200,15 +2487,15 @@ static int process_bad_block(ext2_filsys fs,
         * there's an overlap between the filesystem table blocks
         * (bitmaps and inode table) and the bad block list.
         */
-       if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
-               ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
+       if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
+               ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
                return 0;
        }
        /*
         * Try to find the where the filesystem block was used...
         */
        first_block = fs->super->s_first_data_block;
-       
+
        for (i = 0; i < fs->group_desc_count; i++ ) {
                pctx->group = i;
                pctx->blk = blk;
@@ -2242,22 +2529,22 @@ static int process_bad_block(ext2_filsys fs,
                        return 0;
                }
        skip_super:
-               if (blk == fs->group_desc[i].bg_block_bitmap) {
+               if (blk == ext2fs_block_bitmap_loc(fs, i)) {
                        if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
                                ctx->invalid_block_bitmap_flag[i]++;
                                ctx->invalid_bitmaps++;
                        }
                        return 0;
                }
-               if (blk == fs->group_desc[i].bg_inode_bitmap) {
+               if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
                        if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
                                ctx->invalid_inode_bitmap_flag[i]++;
                                ctx->invalid_bitmaps++;
                        }
                        return 0;
                }
-               if ((blk >= fs->group_desc[i].bg_inode_table) &&
-                   (blk < (fs->group_desc[i].bg_inode_table +
+               if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
+                   (blk < (ext2fs_inode_table_loc(fs, i) +
                            fs->inode_blocks_per_group))) {
                        /*
                         * If there are bad blocks in the inode table,
@@ -2294,13 +2581,14 @@ static int process_bad_block(ext2_filsys fs,
        return 0;
 }
 
-static void new_table_block(e2fsck_t ctx, blk_t first_block, int group, 
-                           const char *name, int num, blk_t *new_block)
+static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
+                           const char *name, int num, blk64_t *new_block)
 {
        ext2_filsys fs = ctx->fs;
-       blk_t           old_block = *new_block;
-       blk_t           last_block;
-       int             i;
+       dgrp_t          last_grp;
+       blk64_t         old_block = *new_block;
+       blk64_t         last_block;
+       int             i, is_flexbg, flexbg, flexbg_size;
        char            *buf;
        struct problem_context  pctx;
 
@@ -2310,19 +2598,44 @@ static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
        pctx.blk = old_block;
        pctx.str = name;
 
-       last_block = ext2fs_group_last_block(fs, group);
-       pctx.errcode = ext2fs_get_free_blocks(fs, first_block, last_block,
-                                       num, ctx->block_found_map, new_block);
+       /*
+        * For flex_bg filesystems, first try to allocate the metadata
+        * within the flex_bg, and if that fails then try finding the
+        * space anywhere in the filesystem.
+        */
+       is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
+                                             EXT4_FEATURE_INCOMPAT_FLEX_BG);
+       if (is_flexbg) {
+               flexbg_size = 1 << fs->super->s_log_groups_per_flex;
+               flexbg = group / flexbg_size;
+               first_block = ext2fs_group_first_block2(fs,
+                                                       flexbg_size * flexbg);
+               last_grp = group | (flexbg_size - 1);
+               if (last_grp > fs->group_desc_count)
+                       last_grp = fs->group_desc_count;
+               last_block = ext2fs_group_last_block2(fs, last_grp);
+       } else
+               last_block = ext2fs_group_last_block2(fs, group);
+       pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
+                                              num, ctx->block_found_map,
+                                              new_block);
+       if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
+               pctx.errcode = ext2fs_get_free_blocks2(fs,
+                               fs->super->s_first_data_block,
+                               ext2fs_blocks_count(fs->super),
+                               num, ctx->block_found_map, new_block);
        if (pctx.errcode) {
                pctx.num = num;
                fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
                ext2fs_unmark_valid(fs);
+               ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
        pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
        if (pctx.errcode) {
                fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
                ext2fs_unmark_valid(fs);
+               ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
        ext2fs_mark_super_dirty(fs);
@@ -2333,9 +2646,9 @@ static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
        pctx.blk2 = 0;
        for (i = 0; i < num; i++) {
                pctx.blk = i;
-               ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
+               ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
                if (old_block) {
-                       pctx.errcode = io_channel_read_blk(fs->io,
+                       pctx.errcode = io_channel_read_blk64(fs->io,
                                   old_block + i, 1, buf);
                        if (pctx.errcode)
                                fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
@@ -2343,7 +2656,7 @@ static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
                        memset(buf, 0, fs->blocksize);
 
                pctx.blk = (*new_block) + i;
-               pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
+               pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
                                              1, buf);
                if (pctx.errcode)
                        fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
@@ -2362,23 +2675,30 @@ static void handle_fs_bad_blocks(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
        dgrp_t          i;
-       blk_t           first_block;
+       blk64_t         first_block;
+       blk64_t         new_blk;
 
        for (i = 0; i < fs->group_desc_count; i++) {
-               first_block = ext2fs_group_first_block(fs, i);
+               first_block = ext2fs_group_first_block2(fs, i);
 
                if (ctx->invalid_block_bitmap_flag[i]) {
+                       new_blk = ext2fs_block_bitmap_loc(fs, i);
                        new_table_block(ctx, first_block, i, _("block bitmap"),
-                                       1, &fs->group_desc[i].bg_block_bitmap);
+                                       1, &new_blk);
+                       ext2fs_block_bitmap_loc_set(fs, i, new_blk);
                }
                if (ctx->invalid_inode_bitmap_flag[i]) {
+                       new_blk = ext2fs_inode_bitmap_loc(fs, i);
                        new_table_block(ctx, first_block, i, _("inode bitmap"),
-                                       1, &fs->group_desc[i].bg_inode_bitmap);
+                                       1, &new_blk);
+                       ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
                }
                if (ctx->invalid_inode_table_flag[i]) {
+                       new_blk = ext2fs_inode_table_loc(fs, i);
                        new_table_block(ctx, first_block, i, _("inode table"),
-                                       fs->inode_blocks_per_group, 
-                                       &fs->group_desc[i].bg_inode_table);
+                                       fs->inode_blocks_per_group,
+                                       &new_blk);
+                       ext2fs_inode_table_loc_set(fs, i, new_blk);
                        ctx->flags |= E2F_FLAG_RESTART;
                }
        }
@@ -2392,13 +2712,13 @@ static void handle_fs_bad_blocks(e2fsck_t ctx)
 static void mark_table_blocks(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       blk_t   b;
+       blk64_t b;
        dgrp_t  i;
        int     j;
        struct problem_context pctx;
-       
+
        clear_problem_context(&pctx);
-       
+
        for (i = 0; i < fs->group_desc_count; i++) {
                pctx.group = i;
 
@@ -2407,61 +2727,62 @@ static void mark_table_blocks(e2fsck_t ctx)
                /*
                 * Mark the blocks used for the inode table
                 */
-               if (fs->group_desc[i].bg_inode_table) {
-                       for (j = 0, b = fs->group_desc[i].bg_inode_table;
+               if (ext2fs_inode_table_loc(fs, i)) {
+                       for (j = 0, b = ext2fs_inode_table_loc(fs, i);
                             j < fs->inode_blocks_per_group;
                             j++, b++) {
-                               if (ext2fs_test_block_bitmap(ctx->block_found_map,
+                               if (ext2fs_test_block_bitmap2(ctx->block_found_map,
                                                             b)) {
                                        pctx.blk = b;
-                                       if (fix_problem(ctx,
+                                       if (!ctx->invalid_inode_table_flag[i] &&
+                                           fix_problem(ctx,
                                                PR_1_ITABLE_CONFLICT, &pctx)) {
                                                ctx->invalid_inode_table_flag[i]++;
                                                ctx->invalid_bitmaps++;
                                        }
                                } else {
-                                   ext2fs_mark_block_bitmap(ctx->block_found_map,
+                                   ext2fs_mark_block_bitmap2(ctx->block_found_map,
                                                             b);
                                }
                        }
                }
-                           
+
                /*
-                * Mark block used for the block bitmap 
+                * Mark block used for the block bitmap
                 */
-               if (fs->group_desc[i].bg_block_bitmap) {
-                       if (ext2fs_test_block_bitmap(ctx->block_found_map,
-                                    fs->group_desc[i].bg_block_bitmap)) {
-                               pctx.blk = fs->group_desc[i].bg_block_bitmap;
+               if (ext2fs_block_bitmap_loc(fs, i)) {
+                       if (ext2fs_test_block_bitmap2(ctx->block_found_map,
+                                    ext2fs_block_bitmap_loc(fs, i))) {
+                               pctx.blk = ext2fs_block_bitmap_loc(fs, i);
                                if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
                                        ctx->invalid_block_bitmap_flag[i]++;
                                        ctx->invalid_bitmaps++;
                                }
                        } else {
-                           ext2fs_mark_block_bitmap(ctx->block_found_map,
-                                    fs->group_desc[i].bg_block_bitmap);
+                           ext2fs_mark_block_bitmap2(ctx->block_found_map,
+                                    ext2fs_block_bitmap_loc(fs, i));
                    }
-                       
+
                }
                /*
-                * Mark block used for the inode bitmap 
+                * Mark block used for the inode bitmap
                 */
-               if (fs->group_desc[i].bg_inode_bitmap) {
-                       if (ext2fs_test_block_bitmap(ctx->block_found_map,
-                                    fs->group_desc[i].bg_inode_bitmap)) {
-                               pctx.blk = fs->group_desc[i].bg_inode_bitmap;
+               if (ext2fs_inode_bitmap_loc(fs, i)) {
+                       if (ext2fs_test_block_bitmap2(ctx->block_found_map,
+                                    ext2fs_inode_bitmap_loc(fs, i))) {
+                               pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
                                if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
                                        ctx->invalid_inode_bitmap_flag[i]++;
                                        ctx->invalid_bitmaps++;
-                               } 
+                               }
                        } else {
-                           ext2fs_mark_block_bitmap(ctx->block_found_map,
-                                    fs->group_desc[i].bg_inode_bitmap);
+                           ext2fs_mark_block_bitmap2(ctx->block_found_map,
+                                    ext2fs_inode_bitmap_loc(fs, i));
                        }
                }
        }
 }
-       
+
 /*
  * Thes subroutines short circuits ext2fs_get_blocks and
  * ext2fs_check_directory; we use them since we already have the inode
@@ -2473,7 +2794,7 @@ static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
 {
        e2fsck_t ctx = (e2fsck_t) fs->priv_data;
        int     i;
-       
+
        if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
                return EXT2_ET_CALLBACK_NOTHANDLED;
 
@@ -2521,13 +2842,17 @@ static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
 {
        e2fsck_t ctx = (e2fsck_t) fs->priv_data;
        errcode_t       retval;
-       blk_t           new_block;
+       blk64_t         new_block;
 
        if (ctx->block_found_map) {
-               retval = ext2fs_new_block(fs, (blk_t) goal, 
-                                         ctx->block_found_map, &new_block);
+               retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
+                                          &new_block);
                if (retval)
                        return retval;
+               if (fs->block_map) {
+                       ext2fs_mark_block_bitmap2(fs->block_map, new_block);
+                       ext2fs_mark_bb_dirty(fs);
+               }
        } else {
                if (!fs->block_map) {
                        retval = ext2fs_read_block_bitmap(fs);
@@ -2535,11 +2860,11 @@ static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
                                return retval;
                }
 
-               retval = ext2fs_new_block(fs, (blk_t) goal, 0, &new_block);
+               retval = ext2fs_new_block2(fs, goal, 0, &new_block);
                if (retval)
                        return retval;
        }
-               
+
        *ret = new_block;
        return (0);
 }
@@ -2550,19 +2875,17 @@ static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
 
        if (ctx->block_found_map) {
                if (inuse > 0)
-                       ext2fs_mark_block_bitmap(ctx->block_found_map, 
-                                                (blk_t) blk);
+                       ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
                else
-                       ext2fs_unmark_block_bitmap(ctx->block_found_map, 
-                                                  (blk_t) blk);
+                       ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
        }
 }
 
-void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
+void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
 {
        ext2_filsys fs = ctx->fs;
 
-       if (bool) {
+       if (use_shortcuts) {
                fs->get_blocks = pass1_get_blocks;
                fs->check_directory = pass1_check_directory;
                fs->read_inode = pass1_read_inode;