Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / e2fsck / pass1.c
index f2f72ed..bd89b96 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,46 +257,45 @@ 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;
        struct ext2_inode_large *inode;
        struct ext2_ext_attr_entry *entry;
-       char *start, *end;
+       char *start;
        unsigned int storage_size, remain;
-       int problem = 0;
+       problem_t problem = 0;
 
        inode = (struct ext2_inode_large *) pctx->inode;
        storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
                inode->i_extra_isize;
        start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
                inode->i_extra_isize + sizeof(__u32);
-       end = (char *) inode + EXT2_INODE_SIZE(ctx->fs->super);
        entry = (struct ext2_ext_attr_entry *) start;
 
        /* 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;
@@ -305,7 +307,7 @@ static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
                remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
 
                /* check value size */
-               if (entry->e_value_size == 0 || entry->e_value_size > remain) {
+               if (entry->e_value_size > remain) {
                        pctx->num = entry->e_value_size;
                        problem = PR_1_ATTR_VALUE_SIZE;
                        goto fix;
@@ -362,11 +364,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 +390,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,42 +403,86 @@ 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, rec_len, 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_block4(ctx->fs, blk, buf, 0, pctx->ino);
        ehandler_operation(0);
        if (retval)
                return;
 
        dirent = (struct ext2_dir_entry *) buf;
-       rec_len = (dirent->rec_len || ctx->fs->blocksize < 65536) ?
-               dirent->rec_len : 65536;
-       if (((dirent->name_len & 0xFF) != 1) ||
+       retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
+       if (retval)
+               return;
+       if ((ext2fs_dirent_name_len(dirent) != 1) ||
            (dirent->name[0] != '.') ||
            (dirent->inode != pctx->ino) ||
            (rec_len < 12) ||
@@ -445,9 +491,10 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
                return;
 
        dirent = (struct ext2_dir_entry *) (buf + rec_len);
-       rec_len = (dirent->rec_len || ctx->fs->blocksize < 65536) ?
-               dirent->rec_len : 65536;
-       if (((dirent->name_len & 0xFF) != 2) ||
+       retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
+       if (retval)
+               return;
+       if ((ext2fs_dirent_name_len(dirent) != 2) ||
            (dirent->name[0] != '.') ||
            (dirent->name[1] != '.') ||
            (rec_len < 12) ||
@@ -456,14 +503,14 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
 
        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, 
-                                   ext2_icount_t *ret)
+void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
+                            ext2_icount_t *ret)
 {
        unsigned int            threshold;
        ext2_ino_t              num_dirs;
@@ -493,12 +540,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;
@@ -510,13 +591,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))
@@ -538,7 +619,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
@@ -549,32 +630,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);
@@ -582,9 +669,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;
@@ -624,11 +716,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) {
@@ -650,9 +751,22 @@ 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 < ext2fs_blocks_count(fs->super))
+               ext2fs_mark_block_bitmap2(ctx->block_found_map,
+                                         fs->super->s_mmp_block);
+
+       /* Set up ctx->lost_and_found if possible */
+       (void) e2fsck_get_lost_and_found(ctx, 0);
+
        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)
@@ -660,11 +774,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;
@@ -674,8 +789,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;
@@ -700,10 +823,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;
@@ -713,7 +836,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;
                        }
@@ -742,12 +865,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");
@@ -756,7 +879,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) {
@@ -767,13 +898,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) {
@@ -786,7 +918,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) {
@@ -816,7 +948,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,
@@ -828,20 +960,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, 
+                           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, 
+                               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_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,
                                                        inode_size, "pass1");
                        }
                } else if (ino < EXT2_FIRST_INODE(fs->super)) {
-                       int     problem = 0;
-                       
-                       ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
+                       problem_t problem = 0;
+
+                       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;
@@ -863,12 +1022,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
@@ -889,10 +1049,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) {
@@ -913,7 +1073,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)) {
@@ -921,8 +1081,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;
@@ -931,12 +1091,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);
@@ -944,7 +1108,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)) {
@@ -959,23 +1123,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)) {
@@ -988,7 +1152,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++;
@@ -1022,13 +1186,27 @@ void e2fsck_pass1(e2fsck_t ctx)
                    (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;
 
@@ -1058,7 +1236,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);
 
@@ -1076,20 +1254,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
@@ -1112,23 +1294,24 @@ 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
+       /*
+        * The l+f inode may have been cleared, so zap it now and
+        * later passes will recalculate it if necessary
+        */
+       ctx->lost_and_found = 0;
+
+       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)
 {
@@ -1137,7 +1320,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)
@@ -1159,7 +1342,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
@@ -1174,7 +1357,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
@@ -1201,11 +1384,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;
 }
 
@@ -1218,9 +1409,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);
@@ -1229,7 +1421,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);
 }
 
 
@@ -1239,11 +1431,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);
@@ -1259,11 +1451,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);
@@ -1280,32 +1472,43 @@ 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);
        }
 }
 
+static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
+                                     unsigned int num)
+{
+       if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
+               ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
+       else
+               while (num--)
+                       mark_block_used(ctx, block++);
+}
+
 /*
  * Adjust the extended attribute block's reference counts at the end
  * of pass 1, either by subtracting out references for EA blocks that
@@ -1313,24 +1516,25 @@ 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_attr3(fs, blk, block_buf,
+                                                    pctx.ino);
                if (pctx.errcode) {
                        fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
                        return;
@@ -1341,10 +1545,12 @@ 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,
-                                                            block_buf);
+                       pctx.errcode = ext2fs_write_ext_attr3(fs, blk,
+                                                            block_buf,
+                                                            pctx.ino);
                        if (pctx.errcode) {
-                               fix_problem(ctx, PR_1_EXTATTR_WRITE, &pctx);
+                               fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
+                                           &pctx);
                                continue;
                        }
                }
@@ -1360,14 +1566,15 @@ 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;
+       int             failed_csum = 0;
 
-       blk = inode->i_file_acl;
+       blk = ext2fs_file_acl_block(fs, inode);
        if (blk == 0)
                return 0;
 
@@ -1380,16 +1587,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);
@@ -1415,7 +1623,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 */
@@ -1438,11 +1646,16 @@ 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_attr3(fs, blk, block_buf, pctx->ino);
+       if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
+               if (fix_problem(ctx, PR_1_EA_BLOCK_CSUM_INVALID, pctx))
+                       goto clear_extattr;
+               failed_csum = 1;
+       }
        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) &&
@@ -1458,7 +1671,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;
        }
@@ -1520,17 +1733,29 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
        }
        region_free(region);
 
+       /*
+        * We only get here if there was no other errors that were fixed.
+        * If there was a checksum fail, ask to correct it.
+        */
+       if (failed_csum &&
+           fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
+               pctx->errcode = ext2fs_write_ext_attr3(fs, blk, block_buf,
+                                                      pctx->ino);
+               if (pctx->errcode)
+                       return 0;
+       }
+
        count = header->h_refcount - 1;
        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;
 }
@@ -1543,7 +1768,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)) ||
@@ -1551,22 +1776,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);
 
@@ -1580,7 +1805,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;
@@ -1589,7 +1814,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;
 }
 
@@ -1602,12 +1827,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
@@ -1615,21 +1840,26 @@ 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);
 }
 
 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                             struct process_block_struct *pb,
-                            blk64_t start_block,
+                            blk64_t start_block, blk64_t end_block,
+                            blk64_t eof_block,
                             ext2_extent_handle_t ehandle)
 {
        struct ext2fs_extent    extent;
-       blk_t                   blk;
+       blk64_t                 blk, last_lblk;
        e2_blkcnt_t             blockcnt;
        unsigned int            i;
        int                     is_dir, is_leaf;
-       errcode_t               problem;
+       problem_t               problem;
        struct ext2_extent_info info;
+       int                     failed_csum;
 
        pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
        if (pctx->errcode)
@@ -1637,36 +1867,74 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
 
        pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
                                          &extent);
-       while (!pctx->errcode && info.num_entries-- > 0) {
+       while ((pctx->errcode == 0 ||
+               pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
+              info.num_entries-- > 0) {
+               failed_csum = 0;
                is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
                is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
+               last_lblk = extent.e_lblk + extent.e_len - 1;
 
                problem = 0;
-               if (extent.e_pblk < ctx->fs->super->s_first_data_block ||
-                   extent.e_pblk >= ctx->fs->super->s_blocks_count)
+               /* Ask to clear a corrupt extent block */
+               if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) {
+                       pctx->blk = extent.e_pblk;
+                       pctx->blk2 = extent.e_lblk;
+                       pctx->num = extent.e_len;
+                       problem = PR_1_EXTENT_CSUM_INVALID;
+                       if (fix_problem(ctx, problem, pctx))
+                               goto fix_problem_now;
+                       failed_csum = 1;
+               }
+
+               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 ((end_block && last_lblk > end_block) &&
+                        (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
+                               last_lblk > eof_block)))
+                       problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
+               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;
+               else if (is_leaf && is_dir &&
+                        ((extent.e_lblk + extent.e_len) >
+                         (1 << (21 - ctx->fs->super->s_log_block_size))))
+                       problem = PR_1_TOOBIG_DIR;
+
+               /* Corrupt but passes checks?  Ask to fix checksum. */
+               if (failed_csum) {
+                       pctx->blk = extent.e_pblk;
+                       pctx->blk2 = extent.e_lblk;
+                       pctx->num = extent.e_len;
+                       problem = 0;
+                       if (fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID,
+                                       pctx))
+                               ext2fs_extent_replace(ehandle, 0, &extent);
+               }
 
                if (problem) {
+report_problem:
                        pctx->blk = extent.e_pblk;
                        pctx->blk2 = extent.e_lblk;
                        pctx->num = extent.e_len;
+                       pctx->blkcount = extent.e_lblk + extent.e_len;
                        if (fix_problem(ctx, problem, pctx)) {
+fix_problem_now:
+                               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;
                                }
+                               ext2fs_extent_fix_parents(ehandle);
                                pctx->errcode = ext2fs_extent_get(ehandle,
                                                                  EXT2_EXTENT_CURRENT,
                                                                  &extent);
@@ -1680,48 +1948,105 @@ 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++;
+                       blk64_t lblk = extent.e_lblk;
+
+                       blk = extent.e_pblk;
                        pctx->errcode = ext2fs_extent_get(ehandle,
                                                  EXT2_EXTENT_DOWN, &extent);
                        if (pctx->errcode) {
-                               printf("Error1: %s on inode %u\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 ||
+                                   pctx->errcode ==
+                                       EXT2_ET_EXTENT_CSUM_INVALID)
+                                       goto report_problem;
+                               return;
                        }
-                       scan_extent_node(ctx, pctx, pb, extent.e_lblk, ehandle);
+                       /* The next extent should match this index's logical start */
+                       if (extent.e_lblk != lblk) {
+                               struct ext2_extent_info e_info;
+
+                               ext2fs_extent_get_info(ehandle, &e_info);
+                               pctx->blk = lblk;
+                               pctx->blk2 = extent.e_lblk;
+                               pctx->num = e_info.curr_level - 1;
+                               problem = PR_1_EXTENT_INDEX_START_INVALID;
+                               if (fix_problem(ctx, problem, pctx))
+                                       ext2fs_extent_fix_parents(ehandle);
+                       }
+                       scan_extent_node(ctx, pctx, pb, extent.e_lblk,
+                                        last_lblk, eof_block, ehandle);
+                       if (pctx->errcode)
+                               return;
                        pctx->errcode = ext2fs_extent_get(ehandle,
                                                  EXT2_EXTENT_UP, &extent);
                        if (pctx->errcode) {
-                               printf("Error1: %s on inode %u\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)
-                               printf(("%6lu: expecting %6lu actual extent "
+                       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,
+                                      (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 <
+                                 (e2_blkcnt_t) 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;
+                       }
+               }
+               if (!ctx->fs->cluster_ratio_bits) {
+                       mark_blocks_used(ctx, extent.e_pblk, extent.e_len);
+                       pb->num_blocks += extent.e_len;
+               }
                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)) ==
+                             ((unsigned) 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;
@@ -1729,11 +2054,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 = extent.e_lblk + extent.e_len - 1;
-               if (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
-                       pb->last_block = start_block;
+               start_block = pb->last_block = last_lblk;
+               if (is_leaf && !is_dir &&
+                   !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
+                       pb->last_init_lblock = last_lblk;
        next:
                pctx->errcode = ext2fs_extent_get(ehandle,
                                                  EXT2_EXTENT_NEXT_SIB,
@@ -1752,11 +2079,13 @@ static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
        ext2_filsys             fs = ctx->fs;
        ext2_ino_t              ino = pctx->ino;
        errcode_t               retval;
+       blk64_t                 eof_lblk;
 
-       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;
        }
@@ -1768,11 +2097,17 @@ static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
                ctx->extent_depth_count[info.max_depth]++;
        }
 
-       scan_extent_node(ctx, pctx, pb, 0, ehandle);
-
-       if (pb->fragmented && pb->num_blocks < fs->super->s_blocks_per_group)
-               ctx->fs_fragmented++;
-
+       eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
+               EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
+       scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, 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);
 }
 
@@ -1787,14 +2122,16 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        struct process_block_struct pb;
        ext2_ino_t      ino = pctx->ino;
        struct ext2_inode *inode = pctx->inode;
-       int             bad_size = 0;
+       unsigned        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;
@@ -1824,19 +2161,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 (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_iterate2(fs, ino,
+               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);
@@ -1845,34 +2189,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)) {
@@ -1884,11 +2212,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");
@@ -1897,18 +2221,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;
@@ -1921,11 +2252,12 @@ 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 (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
                         size > ext2_max_sizes[fs->super->s_log_block_size])
@@ -1933,7 +2265,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                        bad_size = 4;
                else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
                         size >
-                        ((1LL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
+                        ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
                        /* too big for an extent-based file - 32bit ee_block */
                        bad_size = 6;
        }
@@ -1950,9 +2282,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))
+           ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
                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) &&
@@ -1960,11 +2292,18 @@ 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 &&
+           !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
+           !(inode->i_flags & EXT2_INDEX_FL) &&
+           ((inode->i_size / fs->blocksize) >= 3))
+               e2fsck_rehash_dir_later(ctx, ino);
+
 out:
        if (dirty_inode)
                e2fsck_write_inode(ctx, ino, inode, "check_blocks");
@@ -1975,9 +2314,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];
 
@@ -1986,8 +2325,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++) {
@@ -2001,16 +2340,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);
@@ -2026,17 +2365,17 @@ 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;
+       problem_t       problem = 0;
        e2fsck_t        ctx;
 
        p = (struct process_block_struct *) priv_data;
@@ -2065,37 +2404,14 @@ 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
@@ -2103,16 +2419,24 @@ static int process_block(ext2_filsys fs,
         */
        if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
                if (p->previous_block+1 != blk) {
-                       if (ctx->options & E2F_OPT_FRAGCHECK)
-                               printf(_("%6lu: expecting %6lu got phys %6lu (blkcnt %lld)\n"),
-                                      (unsigned long) pctx->ino,
+                       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;
@@ -2120,9 +2444,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) {
@@ -2149,7 +2473,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
@@ -2158,18 +2482,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)) ==
+                    ((unsigned) 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;
@@ -2180,15 +2523,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;
@@ -2200,17 +2543,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;
@@ -2219,16 +2562,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;
@@ -2239,7 +2582,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++;
@@ -2249,15 +2592,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;
@@ -2291,22 +2634,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,
@@ -2343,13 +2686,16 @@ 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, blk64_t first_block, dgrp_t 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;
+       dgrp_t          flexbg;
+       unsigned        flexbg_size;
+       int             i, is_flexbg;
        char            *buf;
        struct problem_context  pctx;
 
@@ -2359,19 +2705,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);
@@ -2382,9 +2753,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);
@@ -2392,7 +2763,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);
@@ -2411,23 +2782,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;
                }
        }
@@ -2441,13 +2819,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;
+       unsigned int    j;
        struct problem_context pctx;
-       
+
        clear_problem_context(&pctx);
-       
+
        for (i = 0; i < fs->group_desc_count; i++) {
                pctx.group = i;
 
@@ -2456,61 +2834,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
@@ -2522,7 +2901,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;
 
@@ -2570,13 +2949,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);
@@ -2584,11 +2967,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);
 }
@@ -2599,19 +2982,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;