Whamcloud - gitweb
e2fsprogs: allow 0-length xattr values in e2fsck
[tools/e2fsprogs.git] / e2fsck / pass1.c
index e1aa029..4095703 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;
        }
@@ -163,30 +166,55 @@ int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
  * Check to make sure a symlink inode is real.  Returns 1 if the symlink
  * checks out, 0 if not.
  */
-int e2fsck_pass1_check_symlink(ext2_filsys fs, struct ext2_inode *inode,
-                              char *buf)
+int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
+                              struct ext2_inode *inode, char *buf)
 {
        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;
 
        if ((inode->i_size_high || inode->i_size == 0) ||
            (inode->i_flags & EXT2_INDEX_FL))
                return 0;
 
-       blocks = ext2fs_inode_data_blocks(fs, inode);
+       if (inode->i_flags & EXT4_EXTENTS_FL) {
+               if (inode->i_size > fs->blocksize)
+                       return 0;
+               if (ext2fs_extent_open2(fs, ino, inode, &handle))
+                       return 0;
+               i = 0;
+               if (ext2fs_extent_get_info(handle, &info) ||
+                   (info.num_entries != 1) ||
+                   (info.max_depth != 0))
+                       goto exit_extent;
+               if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
+                   (extent.e_lblk != 0) ||
+                   (extent.e_len != 1) ||
+                   (extent.e_pblk < fs->super->s_first_data_block) ||
+                   (extent.e_pblk >= ext2fs_blocks_count(fs->super)))
+                       goto exit_extent;
+               i = 1;
+       exit_extent:
+               ext2fs_extent_free(handle);
+               return i;
+       }
+
+       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);
@@ -229,24 +257,24 @@ 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;
 
@@ -255,19 +283,19 @@ static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
                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;
@@ -279,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;
@@ -291,9 +319,12 @@ static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
                        problem = PR_1_ATTR_VALUE_BLOCK;
                        goto fix;
                }
-               
-               /* e_hash must be 0 in inode's ea */
-               if (entry->e_hash != 0) {
+
+               hash = ext2fs_ext_attr_hash_entry(entry,
+                                                 start + entry->e_value_offs);
+
+               /* e_hash may be 0 in older inode's ea */
+               if (entry->e_hash != 0 && entry->e_hash != hash) {
                        pctx->num = entry->e_hash;
                        problem = PR_1_ATTR_HASH;
                        goto fix;
@@ -308,13 +339,10 @@ fix:
         * it seems like a corruption. it's very unlikely we could repair
         * EA(s) in automatic fashion -bzzz
         */
-#if 0
-       problem = PR_1_ATTR_HASH;
-#endif
        if (problem == 0 || !fix_problem(ctx, problem, pctx))
                return;
 
-       /* simple remove all possible EA(s) */
+       /* simply remove all possible EA(s) */
        *((__u32 *)start) = 0UL;
        e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
                                EXT2_INODE_SIZE(sb), "pass1");
@@ -336,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
         */
@@ -362,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
@@ -375,64 +403,113 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
 {
        struct ext2_inode *inode = pctx->inode;
        struct ext2_dir_entry   *dirent;
-       const char              *old_op;
        errcode_t               retval;
-       blk_t                   blk;
-       int                     i, not_device = 0;
+       blk64_t                 blk;
+       unsigned int            i, rec_len, not_device = 0;
+       int                     extent_fs;
 
+       /*
+        * If the mode looks OK, we believe it.  If the first block in
+        * the i_block array is 0, this cannot be a directory. If the
+        * inode is extent-mapped, it is still the case that the latter
+        * cannot be 0 - the magic number in the extent header would make
+        * it nonzero.
+        */
        if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
            LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
                return;
 
-       for (i=0; i < EXT2_N_BLOCKS; i++) {
-               blk = inode->i_block[i];
-               if (!blk)
-                       continue;
-               if (i >= 4)
-                       not_device++;
+       /* 
+        * Check the block numbers in the i_block array for validity:
+        * zero blocks are skipped (but the first one cannot be zero -
+        * see above), other blocks are checked against the first and
+        * max data blocks (from the the superblock) and against the
+        * block bitmap. Any invalid block found means this cannot be
+        * a directory.
+        * 
+        * If there are non-zero blocks past the fourth entry, then
+        * this cannot be a device file: we remember that for the next
+        * check.
+        *
+        * For extent mapped files, we don't do any sanity checking:
+        * just try to get the phys block of logical block 0 and run
+        * with it.
+        */
 
-               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 */
+       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 >= ext2fs_blocks_count(ctx->fs->super) ||
+                           ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
+                                                          blk))
+                               return; /* Invalid block, can't be dir */
+               }
+               blk = inode->i_block[0];
        }
 
-       if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) && 
+       /*
+        * If the mode says this is a device file and the i_links_count field
+        * is sane and we have not ruled it out as a device file previously,
+        * we declare it a device file, not a directory.
+        */
+       if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
            (inode->i_links_count == 1) && !not_device)
                return;
 
-       old_op = ehandler_operation(_("reading directory block"));
-       retval = ext2fs_read_dir_block(ctx->fs, inode->i_block[0], buf);
+       /* read the first block */
+       ehandler_operation(_("reading directory block"));
+       retval = ext2fs_read_dir_block3(ctx->fs, blk, buf, 0);
        ehandler_operation(0);
        if (retval)
                return;
 
        dirent = (struct ext2_dir_entry *) buf;
+       retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
+       if (retval)
+               return;
        if (((dirent->name_len & 0xFF) != 1) ||
            (dirent->name[0] != '.') ||
            (dirent->inode != pctx->ino) ||
-           (dirent->rec_len < 12) ||
-           (dirent->rec_len % 4) ||
-           (dirent->rec_len >= ctx->fs->blocksize - 12))
+           (rec_len < 12) ||
+           (rec_len % 4) ||
+           (rec_len >= ctx->fs->blocksize - 12))
                return;
 
-       dirent = (struct ext2_dir_entry *) (buf + dirent->rec_len);
+       dirent = (struct ext2_dir_entry *) (buf + rec_len);
+       retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
+       if (retval)
+               return;
        if (((dirent->name_len & 0xFF) != 2) ||
            (dirent->name[0] != '.') ||
            (dirent->name[1] != '.') ||
-           (dirent->rec_len < 12) ||
-           (dirent->rec_len % 4))
+           (rec_len < 12) ||
+           (rec_len % 4))
                return;
 
        if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
                inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
-               e2fsck_write_inode_full(ctx, pctx->ino, inode, 
-                                       EXT2_INODE_SIZE(ctx->fs->super), 
+               e2fsck_write_inode_full(ctx, pctx->ino, inode,
+                                       EXT2_INODE_SIZE(ctx->fs->super),
                                        "check_is_really_dir");
        }
 }
 
-extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags, 
+extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
                                    ext2_icount_t *ret)
 {
        unsigned int            threshold;
@@ -468,7 +545,7 @@ 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;
@@ -480,13 +557,12 @@ void e2fsck_pass1(e2fsck_t ctx)
        struct          scan_callback_struct scan_struct;
        struct ext2_super_block *sb = ctx->fs->super;
        const char      *old_op;
-       int             imagic_fs;
+       unsigned int    save_type;
+       int             imagic_fs, extent_fs;
        int             busted_fs_time = 0;
        int             inode_size;
-       
-#ifdef RESOURCE_TRACK
+
        init_resource_track(&rtrack, ctx->fs->io);
-#endif
        clear_problem_context(&pctx);
 
        if (!(ctx->options & E2F_OPT_PREEN))
@@ -508,42 +584,49 @@ 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
 
        imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
+       extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
 
        /*
         * 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);
@@ -551,9 +634,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;
@@ -593,11 +681,19 @@ 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);
        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) {
@@ -619,9 +715,19 @@ void e2fsck_pass1(e2fsck_t ctx)
            (fs->super->s_mtime < fs->super->s_inodes_count))
                busted_fs_time = 1;
 
+       if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
+           !(fs->super->s_mmp_block <= fs->super->s_first_data_block ||
+             fs->super->s_mmp_block >= fs->super->s_blocks_count))
+               ext2fs_mark_block_bitmap2(ctx->block_found_map,
+                                         fs->super->s_mmp_block);
+
        while (1) {
+               if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
+                       if (e2fsck_mmp_update(fs))
+                               fatal_error(ctx, 0);
+               }
                old_op = ehandler_operation(_("getting next inode from scan"));
-               pctx.errcode = ext2fs_get_next_inode_full(scan, &ino, 
+               pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
                                                          inode, inode_size);
                ehandler_operation(old_op);
                if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
@@ -629,8 +735,8 @@ 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) {
@@ -644,7 +750,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                pctx.inode = inode;
                ctx->stashed_ino = ino;
                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;
@@ -653,9 +759,87 @@ void e2fsck_pass1(e2fsck_t ctx)
                                return;
                        }
                }
+
+               /*
+                * Test for incorrect extent flag settings.
+                *
+                * On big-endian machines we must be careful:
+                * When the inode is read, the i_block array is not swapped
+                * if the extent flag is set.  Therefore if we are testing
+                * for or fixing a wrongly-set flag, we must potentially
+                * (un)swap before testing, or after fixing.
+                */
+
+               /*
+                * In this case the extents flag was set when read, so
+                * 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 &&
+                   (inode->i_links_count || (ino == EXT2_BAD_INO) ||
+                    (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
+                       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;
+                               ext2fs_mark_super_dirty(fs);
+                               extent_fs = 1;
+                       } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
+                       clear_inode:
+                               e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
+                               if (ino == EXT2_BAD_INO)
+                                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
+                                                                ino);
+                               continue;
+                       }
+               }
+
+               /*
+                * For big-endian machines:
+                * If the inode didn't have the extents flag set when it
+                * was read, then the i_blocks array was swapped.  To test
+                * as an extents header, we must swap it back first.
+                * IF we then set the extents flag, the entire i_block
+                * array must be un/re-swapped to make it proper extents data.
+                */
+               if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
+                   (inode->i_links_count || (ino == EXT2_BAD_INO) ||
+                    (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
+                   (LINUX_S_ISREG(inode->i_mode) ||
+                    LINUX_S_ISDIR(inode->i_mode))) {
+                       void *ehp;
+#ifdef WORDS_BIGENDIAN
+                       __u32 tmp_block[EXT2_N_BLOCKS];
+
+                       for (i = 0; i < EXT2_N_BLOCKS; i++)
+                               tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
+                       ehp = tmp_block;
+#else
+                       ehp = inode->i_block;
+#endif
+                       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,
+                                      sizeof(inode->i_block));
+#endif
+                               e2fsck_write_inode(ctx, ino, inode, "pass1");
+                       }
+               }
+
                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) {
@@ -666,13 +850,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) {
@@ -685,7 +870,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) {
@@ -695,10 +880,8 @@ void e2fsck_pass1(e2fsck_t ctx)
                         * regnerated in pass #3.
                         */
                        if (!LINUX_S_ISDIR(inode->i_mode)) {
-                               if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
-                                       e2fsck_clear_inode(ctx, ino, inode,
-                                                          0, "pass1");
-                               }
+                               if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
+                                       goto clear_inode;
                        }
                        /*
                         * If dtime is set, offer to clear it.  mke2fs
@@ -717,7 +900,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,
@@ -729,20 +912,47 @@ void e2fsck_pass1(e2fsck_t ctx)
                                check_blocks(ctx, &pctx, block_buf);
                                continue;
                        }
-                       if ((inode->i_links_count || inode->i_blocks ||
+                       if ((inode->i_links_count ||
+                            inode->i_blocks || inode->i_block[0]) &&
+                           fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
+                                       &pctx)) {
+                               memset(inode, 0, inode_size);
+                               ext2fs_icount_store(ctx->inode_link_info,
+                                                   ino, 0);
+                               e2fsck_write_inode_full(ctx, ino, inode,
+                                                       inode_size, "pass1");
+                       }
+               } else if ((ino == EXT4_USR_QUOTA_INO) ||
+                          (ino == EXT4_GRP_QUOTA_INO)) {
+                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
+                       if ((fs->super->s_feature_ro_compat &
+                                       EXT4_FEATURE_RO_COMPAT_QUOTA) &&
+                           ((fs->super->s_usr_quota_inum == ino) ||
+                            (fs->super->s_grp_quota_inum == ino))) {
+                               if (!LINUX_S_ISREG(inode->i_mode) &&
+                                   fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
+                                                       &pctx)) {
+                                       inode->i_mode = LINUX_S_IFREG;
+                                       e2fsck_write_inode(ctx, ino, inode,
+                                                       "pass1");
+                               }
+                               check_blocks(ctx, &pctx, block_buf);
+                               continue;
+                       }
+                       if ((inode->i_links_count ||
                             inode->i_blocks || inode->i_block[0]) &&
-                           fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR, 
+                           fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
                                        &pctx)) {
                                memset(inode, 0, inode_size);
                                ext2fs_icount_store(ctx->inode_link_info,
                                                    ino, 0);
-                               e2fsck_write_inode_full(ctx, ino, inode, 
+                               e2fsck_write_inode_full(ctx, ino, inode,
                                                        inode_size, "pass1");
                        }
                } else if (ino < EXT2_FIRST_INODE(fs->super)) {
                        int     problem = 0;
-                       
-                       ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
+
+                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
                        if (ino == EXT2_BOOT_LOADER_INO) {
                                if (LINUX_S_ISDIR(inode->i_mode))
                                        problem = PR_1_RESERVED_BAD_MODE;
@@ -764,12 +974,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
@@ -790,10 +1001,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) {
@@ -814,7 +1025,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)) {
@@ -822,8 +1033,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;
@@ -832,12 +1043,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);
@@ -845,7 +1060,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)) {
@@ -859,12 +1074,24 @@ void e2fsck_pass1(e2fsck_t ctx)
                check_inode_extra_space(ctx, &pctx);
                check_is_really_dir(ctx, &pctx, block_buf);
 
+               /*
+                * 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_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)) {
@@ -877,7 +1104,8 @@ 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, inode, block_buf)) {
+                          e2fsck_pass1_check_symlink(fs, ino, inode,
+                                                     block_buf)) {
                        check_immutable(ctx, &pctx);
                        ctx->fs_symlinks_count++;
                        if (ext2fs_inode_data_blocks(fs, inode) == 0) {
@@ -898,16 +1126,19 @@ void e2fsck_pass1(e2fsck_t ctx)
                        ctx->fs_sockets_count++;
                } else
                        mark_inode_bad(ctx, ino);
-               if (inode->i_block[EXT2_IND_BLOCK])
-                       ctx->fs_ind_count++;
-               if (inode->i_block[EXT2_DIND_BLOCK])
-                       ctx->fs_dind_count++;
-               if (inode->i_block[EXT2_TIND_BLOCK])
-                       ctx->fs_tind_count++;
-               if (inode->i_block[EXT2_IND_BLOCK] ||
-                   inode->i_block[EXT2_DIND_BLOCK] ||
-                   inode->i_block[EXT2_TIND_BLOCK] ||
-                   inode->i_file_acl) {
+               if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
+                       if (inode->i_block[EXT2_IND_BLOCK])
+                               ctx->fs_ind_count++;
+                       if (inode->i_block[EXT2_DIND_BLOCK])
+                               ctx->fs_dind_count++;
+                       if (inode->i_block[EXT2_TIND_BLOCK])
+                               ctx->fs_tind_count++;
+               }
+               if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
+                   (inode->i_block[EXT2_IND_BLOCK] ||
+                    inode->i_block[EXT2_DIND_BLOCK] ||
+                    inode->i_block[EXT2_TIND_BLOCK] ||
+                    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++;
@@ -943,7 +1174,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);
 
@@ -961,20 +1192,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
@@ -997,23 +1232,18 @@ void e2fsck_pass1(e2fsck_t ctx)
        ext2fs_free_mem(&inodes_to_process);
 endit:
        e2fsck_use_inode_shortcuts(ctx, 0);
-       
+
        ext2fs_free_mem(&block_buf);
        ext2fs_free_mem(&inode);
 
-#ifdef RESOURCE_TRACK
-       if (ctx->options & E2F_OPT_TIME2) {
-               e2fsck_clear_progbar(ctx);
-               print_resource_track(_("Pass 1"), &rtrack, ctx->fs->io);
-       }
-#endif
+       print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
 }
 
 /*
  * When the inode_scan routines call this callback at the end of the
  * glock group, call process_inodes.
  */
-static errcode_t scan_callback(ext2_filsys fs, 
+static errcode_t scan_callback(ext2_filsys fs,
                               ext2_inode_scan scan EXT2FS_ATTR((unused)),
                               dgrp_t group, void * priv_data)
 {
@@ -1022,7 +1252,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)
@@ -1044,7 +1274,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
@@ -1059,7 +1289,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
@@ -1086,11 +1316,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;
 }
 
@@ -1103,9 +1341,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);
@@ -1114,7 +1353,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);
 }
 
 
@@ -1124,11 +1363,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);
@@ -1144,11 +1383,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);
@@ -1165,32 +1404,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
@@ -1198,24 +1448,24 @@ static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
  * EA blocks that had extra references as accounted for in
  * ctx->refcount_extra.
  */
-static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount, 
+static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
                                    char *block_buf, int adjust_sign)
 {
        struct ext2_ext_attr_header     *header;
        struct problem_context          pctx;
        ext2_filsys                     fs = ctx->fs;
-       blk_t                           blk;
+       blk64_t                         blk;
        __u32                           should_be;
        int                             count;
 
        clear_problem_context(&pctx);
-       
+
        ea_refcount_intr_begin(refcount);
        while (1) {
                if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
                        break;
                pctx.blk = blk;
-               pctx.errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
+               pctx.errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
                if (pctx.errcode) {
                        fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
                        return;
@@ -1226,10 +1476,11 @@ static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
                pctx.num = should_be;
                if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
                        header->h_refcount = should_be;
-                       pctx.errcode = ext2fs_write_ext_attr(fs, blk,
+                       pctx.errcode = ext2fs_write_ext_attr2(fs, blk,
                                                             block_buf);
                        if (pctx.errcode) {
-                               fix_problem(ctx, PR_1_EXTATTR_WRITE, &pctx);
+                               fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
+                                           &pctx);
                                continue;
                        }
                }
@@ -1245,14 +1496,14 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
        ext2_filsys fs = ctx->fs;
        ext2_ino_t      ino = pctx->ino;
        struct ext2_inode *inode = pctx->inode;
-       blk_t           blk;
+       blk64_t         blk;
        char *          end;
        struct ext2_ext_attr_header *header;
        struct ext2_ext_attr_entry *entry;
        int             count;
        region_t        region = 0;
 
-       blk = inode->i_file_acl;
+       blk = ext2fs_file_acl_block(fs, inode);
        if (blk == 0)
                return 0;
 
@@ -1265,16 +1516,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);
@@ -1300,7 +1552,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 */
@@ -1323,11 +1575,11 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
         * validate it
         */
        pctx->blk = blk;
-       pctx->errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
+       pctx->errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
        if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
                goto clear_extattr;
        header = (struct ext2_ext_attr_header *) block_buf;
-       pctx->blk = inode->i_file_acl;
+       pctx->blk = ext2fs_file_acl_block(fs, inode);
        if (((ctx->ext_attr_ver == 1) &&
             (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
            ((ctx->ext_attr_ver == 2) &&
@@ -1343,7 +1595,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;
        }
@@ -1355,10 +1607,13 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
        entry = (struct ext2_ext_attr_entry *)(header+1);
        end = block_buf + fs->blocksize;
        while ((char *)entry < end && *(__u32 *)entry) {
+               __u32 hash;
+
                if (region_allocate(region, (char *)entry - (char *)header,
                                   EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
                        if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
                                goto clear_extattr;
+                       break;
                }
                if ((ctx->ext_attr_ver == 1 &&
                     (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
@@ -1366,6 +1621,7 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
                     entry->e_name_index == 0)) {
                        if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
                                goto clear_extattr;
+                       break;
                }
                if (entry->e_value_block != 0) {
                        if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
@@ -1382,6 +1638,17 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
                        if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
                                goto clear_extattr;
                }
+
+               hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
+                                                        entry->e_value_offs);
+
+               if (entry->e_hash != hash) {
+                       pctx->num = entry->e_hash;
+                       if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
+                               goto clear_extattr;
+                       entry->e_hash = hash;
+               }
+
                entry = EXT2_EXT_ATTR_NEXT(entry);
        }
        if (region_allocate(region, (char *)entry - (char *)header, 4)) {
@@ -1394,27 +1661,26 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
        if (count)
                ea_refcount_store(ctx->refcount, blk, count);
        mark_block_used(ctx, blk);
-       ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk);
+       ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
        return 1;
 
 clear_extattr:
        if (region)
                region_free(region);
-       inode->i_file_acl = 0;
+       ext2fs_file_acl_block_set(fs, inode, 0);
        e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
        return 0;
 }
 
 /* Returns 1 if bad htree, 0 if OK */
 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
-                       ext2_ino_t ino EXT2FS_ATTR((unused)),
-                       struct ext2_inode *inode,
+                       ext2_ino_t ino, struct ext2_inode *inode,
                        char *block_buf)
 {
        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)) ||
@@ -1422,17 +1688,22 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
             fix_problem(ctx, PR_1_HTREE_SET, pctx)))
                return 1;
 
-       blk = inode->i_block[0];
-       if (((blk == 0) ||
-            (blk < fs->super->s_first_data_block) ||
-            (blk >= fs->super->s_blocks_count)) &&
-           fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
-               return 1;
+       pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
 
-       retval = io_channel_read_blk(fs->io, blk, 1, block_buf);
+       if ((pctx->errcode) ||
+           (blk == 0) ||
+           (blk < fs->super->s_first_data_block) ||
+           (blk >= ext2fs_blocks_count(fs->super))) {
+               if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
+                       return 1;
+               else
+                       return 0;
+       }
+
+       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);
 
@@ -1446,7 +1717,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;
@@ -1455,7 +1726,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;
 }
 
@@ -1463,17 +1734,17 @@ void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
                        struct ext2_inode *inode, int restart_flag,
                        const char *source)
 {
-
+       inode->i_flags = 0;
        inode->i_links_count = 0;
        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
@@ -1481,9 +1752,232 @@ 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,
+                            ext2_extent_handle_t ehandle)
+{
+       struct ext2fs_extent    extent;
+       blk64_t                 blk;
+       e2_blkcnt_t             blockcnt;
+       unsigned int            i;
+       int                     is_dir, is_leaf;
+       errcode_t               problem;
+       struct ext2_extent_info info;
+
+       pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
+       if (pctx->errcode)
+               return;
+
+       pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
+                                         &extent);
+       while (!pctx->errcode && info.num_entries-- > 0) {
+               is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
+               is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
+
+               problem = 0;
+               if (extent.e_pblk == 0 ||
+                   extent.e_pblk < ctx->fs->super->s_first_data_block ||
+                   extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
+                       problem = PR_1_EXTENT_BAD_START_BLK;
+               else if (extent.e_lblk < start_block)
+                       problem = PR_1_OUT_OF_ORDER_EXTENTS;
+               else if (is_leaf && extent.e_len == 0)
+                       problem = PR_1_EXTENT_LENGTH_ZERO;
+               else if (is_leaf &&
+                        (extent.e_pblk + extent.e_len) >
+                        ext2fs_blocks_count(ctx->fs->super))
+                       problem = PR_1_EXTENT_ENDS_BEYOND;
+
+               if (problem) {
+report_problem:
+                       pctx->blk = extent.e_pblk;
+                       pctx->blk2 = extent.e_lblk;
+                       pctx->num = extent.e_len;
+                       if (fix_problem(ctx, problem, pctx)) {
+                               e2fsck_read_bitmaps(ctx);
+                               pctx->errcode =
+                                       ext2fs_extent_delete(ehandle, 0);
+                               if (pctx->errcode) {
+                                       pctx->str = "ext2fs_extent_delete";
+                                       return;
+                               }
+                               ext2fs_extent_fix_parents(ehandle);
+                               pctx->errcode = ext2fs_extent_get(ehandle,
+                                                                 EXT2_EXTENT_CURRENT,
+                                                                 &extent);
+                               if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
+                                       pctx->errcode = 0;
+                                       break;
+                               }
+                               continue;
+                       }
+                       goto next;
+               }
+
+               if (!is_leaf) {
+                       blk64_t lblk;
+
+                       blk = extent.e_pblk;
+                       lblk = extent.e_lblk;
+                       pctx->errcode = ext2fs_extent_get(ehandle,
+                                                 EXT2_EXTENT_DOWN, &extent);
+                       if (pctx->errcode) {
+                               pctx->str = "EXT2_EXTENT_DOWN";
+                               problem = PR_1_EXTENT_HEADER_INVALID;
+                               if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
+                                       goto report_problem;
+                               return;
+                       }
+                       /* The next extent should match this index's logical start */
+                       if (extent.e_lblk != lblk) {
+                               struct ext2_extent_info info;
+
+                               ext2fs_extent_get_info(ehandle, &info);
+                               pctx->blk = lblk;
+                               pctx->blk2 = extent.e_lblk;
+                               pctx->num = 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, ehandle);
+                       if (pctx->errcode)
+                               return;
+                       pctx->errcode = ext2fs_extent_get(ehandle,
+                                                 EXT2_EXTENT_UP, &extent);
+                       if (pctx->errcode) {
+                               pctx->str = "EXT2_EXTENT_UP";
+                               return;
+                       }
+                       mark_block_used(ctx, blk);
+                       pb->num_blocks++;
+                       goto next;
+               }
+
+               if ((pb->previous_block != 0) &&
+                   (pb->previous_block+1 != extent.e_pblk)) {
+                       if (ctx->options & E2F_OPT_FRAGCHECK) {
+                               char type = '?';
+
+                               if (pb->is_dir)
+                                       type = 'd';
+                               else if (pb->is_reg)
+                                       type = 'f';
+
+                               printf(("%6lu(%c): expecting %6lu "
+                                       "actual extent "
+                                       "phys %6lu log %lu len %lu\n"),
+                                      (unsigned long) pctx->ino, type,
+                                      (unsigned long) pb->previous_block+1,
+                                      (unsigned long) extent.e_pblk,
+                                      (unsigned long) extent.e_lblk,
+                                      (unsigned long) extent.e_len);
+                       }
+                       pb->fragmented = 1;
+               }
+               while (is_dir && ++pb->last_db_block < extent.e_lblk) {
+                       pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
+                                                             pb->ino, 0,
+                                                             pb->last_db_block);
+                       if (pctx->errcode) {
+                               pctx->blk = 0;
+                               pctx->num = pb->last_db_block;
+                               goto failed_add_dir_block;
+                       }
+               }
+               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++) {
+                       if (ctx->fs->cluster_ratio_bits &&
+                           !(pb->previous_block &&
+                             (EXT2FS_B2C(ctx->fs, blk) ==
+                              EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
+                             (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
+                             (blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
+                               mark_block_used(ctx, blk);
+                               pb->num_blocks++;
+                       }
+
+                       pb->previous_block = blk;
+
+                       if (is_dir) {
+                               pctx->errcode = ext2fs_add_dir_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;
+                                       return;
+                               }
+                       }
+               }
+               if (is_dir && extent.e_len > 0)
+                       pb->last_db_block = blockcnt - 1;
+               pb->previous_block = extent.e_pblk + extent.e_len - 1;
+               start_block = pb->last_block = extent.e_lblk + extent.e_len - 1;
+               if (is_leaf && !is_dir &&
+                   !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
+                       pb->last_init_lblock = extent.e_lblk + extent.e_len - 1;
+       next:
+               pctx->errcode = ext2fs_extent_get(ehandle,
+                                                 EXT2_EXTENT_NEXT_SIB,
+                                                 &extent);
+       }
+       if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
+               pctx->errcode = 0;
+}
+
+static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
+                                struct process_block_struct *pb)
+{
+       struct ext2_extent_info info;
+       struct ext2_inode       *inode = pctx->inode;
+       ext2_extent_handle_t    ehandle;
+       ext2_filsys             fs = ctx->fs;
+       ext2_ino_t              ino = pctx->ino;
+       errcode_t               retval;
+
+       pctx->errcode = ext2fs_extent_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;
+       }
+
+       retval = ext2fs_extent_get_info(ehandle, &info);
+       if (retval == 0) {
+               if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
+                       info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
+               ctx->extent_depth_count[info.max_depth]++;
+       }
+
+       scan_extent_node(ctx, pctx, pb, 0, ehandle);
+       if (pctx->errcode &&
+           fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
+               pb->num_blocks = 0;
+               inode->i_blocks = 0;
+               e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
+                                  "check_blocks_extents");
+               pctx->errcode = 0;
+       }
+       ext2fs_extent_free(ehandle);
+}
+
 /*
  * This subroutine is called on each inode to account for all of the
  * blocks used by that inode.
@@ -1497,11 +1991,14 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        struct ext2_inode *inode = pctx->inode;
        int             bad_size = 0;
        int             dirty_inode = 0;
+       int             extent_fs;
        __u64           size;
-       
+
        pb.ino = ino;
        pb.num_blocks = 0;
        pb.last_block = -1;
+       pb.last_init_lblock = -1;
+       pb.last_db_block = -1;
        pb.num_illegal_blocks = 0;
        pb.suppress = 0; pb.clear = 0;
        pb.fragmented = 0;
@@ -1516,6 +2013,9 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        pctx->ino = ino;
        pctx->errcode = 0;
 
+       extent_fs = (ctx->fs->super->s_feature_incompat &
+                     EXT3_FEATURE_INCOMPAT_EXTENTS);
+
        if (inode->i_flags & EXT2_COMPRBLK_FL) {
                if (fs->super->s_feature_incompat &
                    EXT2_FEATURE_INCOMPAT_COMPRESSION)
@@ -1528,13 +2028,27 @@ 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))
-               pctx->errcode = ext2fs_block_iterate2(fs, ino,
-                                      pb.is_dir ? BLOCK_FLAG_HOLE : 0,
-                                      block_buf, process_block, &pb);
+       if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
+               if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
+                       check_blocks_extents(ctx, pctx, &pb);
+               else {
+                       pctx->errcode = ext2fs_block_iterate3(fs, ino,
+                                               pb.is_dir ? BLOCK_FLAG_HOLE : 0,
+                                               block_buf, process_block, &pb);
+                       /*
+                        * We do not have uninitialized extents in non extent
+                        * files.
+                        */
+                       pb.last_init_lblock = pb.last_block;
+               }
+       }
        end_problem_latch(ctx, PR_LATCH_BLOCK);
        end_problem_latch(ctx, PR_LATCH_TOOBIG);
        if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
@@ -1542,15 +2056,19 @@ 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 (inode->i_flags & EXT2_INDEX_FL) {
                if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
                        inode->i_flags &= ~EXT2_INDEX_FL;
@@ -1561,11 +2079,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");
@@ -1574,15 +2088,27 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                }
        }
 
-       pb.num_blocks *= (fs->blocksize / 512);
+       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 (nblock > (pb.last_block + 1))
+               if (inode->i_size & (fs->blocksize - 1))
+                       bad_size = 5;
+               else if (nblock > (pb.last_block + 1))
                        bad_size = 1;
                else if (nblock < (pb.last_block + 1)) {
                        if (((pb.last_block + 1) - nblock) >
@@ -1593,18 +2119,27 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                e2_blkcnt_t blkpg = ctx->blocks_per_page;
 
                size = EXT2_I_SIZE(inode);
-               if ((pb.last_block >= 0) &&
+               if ((pb.last_init_lblock >= 0) &&
                    /* allow allocated blocks to end of PAGE_SIZE */
-                   (size < (__u64)pb.last_block * fs->blocksize) &&
-                   (pb.last_block / blkpg * blkpg != pb.last_block ||
-                    size < (__u64)(pb.last_block & ~(blkpg-1)) *fs->blocksize))
+                   (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
+                   (pb.last_init_lblock / blkpg * blkpg != pb.last_init_lblock ||
+                    size < (__u64)(pb.last_init_lblock & ~(blkpg-1)) *
+                    fs->blocksize))
                        bad_size = 3;
-               else if (size > ext2_max_sizes[fs->super->s_log_block_size])
+               else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
+                        size > ext2_max_sizes[fs->super->s_log_block_size])
+                       /* too big for a direct/indirect-mapped file */
                        bad_size = 4;
+               else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
+                        size >
+                        ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
+                       /* too big for an extent-based file - 32bit ee_block */
+                       bad_size = 6;
        }
        /* i_size for symlinks is checked elsewhere */
        if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
                pctx->num = (pb.last_block+1) * fs->blocksize;
+               pctx->group = bad_size;
                if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
                        inode->i_size = pctx->num;
                        if (!LINUX_S_ISDIR(inode->i_mode))
@@ -1613,17 +2148,27 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                }
                pctx->num = 0;
        }
-       if (LINUX_S_ISREG(inode->i_mode) &&
-           (inode->i_size_high || inode->i_size & 0x80000000UL))
+       if (LINUX_S_ISREG(inode->i_mode) && EXT2_I_SIZE(inode) >= 0x80000000UL)
                ctx->large_files++;
-       if (pb.num_blocks != inode->i_blocks) {
+       if ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
+           ((fs->super->s_feature_ro_compat &
+             EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
+            (inode->i_flags & EXT4_HUGE_FILE_FL) &&
+            (inode->osd2.linux2.l_i_blocks_hi != 0))) {
                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 = pb.num_blocks >> 32;
                        dirty_inode++;
                }
                pctx->num = 0;
        }
+
+       if (ctx->dirs_to_hash && pb.is_dir &&
+           !(inode->i_flags & EXT2_INDEX_FL) &&
+           ((inode->i_size / fs->blocksize) >= 3))
+               ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
+
 out:
        if (dirty_inode)
                e2fsck_write_inode(ctx, ino, inode, "check_blocks");
@@ -1634,9 +2179,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];
 
@@ -1645,8 +2190,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++) {
@@ -1660,16 +2205,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);
@@ -1685,15 +2230,15 @@ static char *describe_illegal_block(ext2_filsys fs, blk_t block)
  * This is a helper function for check_blocks().
  */
 static int process_block(ext2_filsys fs,
-                 blk_t *block_nr,
+                 blk64_t       *block_nr,
                  e2_blkcnt_t blockcnt,
-                 blk_t ref_block EXT2FS_ATTR((unused)),
+                 blk64_t ref_block EXT2FS_ATTR((unused)),
                  int ref_offset EXT2FS_ATTR((unused)),
                  void *priv_data)
 {
        struct process_block_struct *p;
        struct problem_context *pctx;
-       blk_t   blk = *block_nr;
+       blk64_t blk = *block_nr;
        int     ret_code = 0;
        int     problem = 0;
        e2fsck_t        ctx;
@@ -1724,46 +2269,39 @@ static int process_block(ext2_filsys fs,
                return 0;
        }
 
-       if (blk == 0) {
-               if (p->is_dir == 0) {
-                       /*
-                        * Should never happen, since only directories
-                        * get called with BLOCK_FLAG_HOLE
-                        */
-#if DEBUG_E2FSCK
-                       printf("process_block() called with blk == 0, "
-                              "blockcnt=%d, inode %lu???\n",
-                              blockcnt, p->ino);
-#endif
-                       return 0;
-               }
-               if (blockcnt < 0)
-                       return 0;
-               if (blockcnt * fs->blocksize < p->inode->i_size) {
-#if 0
-                       printf("Missing block (#%d) in directory inode %lu!\n",
-                              blockcnt, p->ino);
-#endif
-                       goto mark_dir;
-               }
+       if (blk == 0)
                return 0;
-       }
 
 #if 0
        printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
               blockcnt);
 #endif
-       
+
        /*
         * Simplistic fragmentation check.  We merely require that the
         * file be contiguous.  (Which can never be true for really
         * big files that are greater than a block group.)
         */
-       if (!HOLE_BLKADDR(p->previous_block)) {
-               if (p->previous_block+1 != blk)
+       if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
+               if (p->previous_block+1 != blk) {
+                       if (ctx->options & E2F_OPT_FRAGCHECK) {
+                               char type = '?';
+
+                               if (p->is_dir)
+                                       type = 'd';
+                               else if (p->is_reg)
+                                       type = 'f';
+
+                               printf(_("%6lu(%c): expecting %6lu "
+                                        "got phys %6lu (blkcnt %lld)\n"),
+                                      (unsigned long) pctx->ino, type,
+                                      (unsigned long) p->previous_block+1,
+                                      (unsigned long) blk,
+                                      blockcnt);
+                       }
                        p->fragmented = 1;
+               }
        }
-       p->previous_block = blk;
 
        if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
                problem = PR_1_TOOBIG_DIR;
@@ -1771,9 +2309,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) {
@@ -1800,7 +2338,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
@@ -1809,18 +2347,37 @@ static int process_block(ext2_filsys fs,
                 */
                if (blockcnt == BLOCK_COUNT_DIND)
                        mark_block_used(ctx, blk);
-       } else
+               p->num_blocks++;
+       } else if (!(ctx->fs->cluster_ratio_bits &&
+                    p->previous_block &&
+                    (EXT2FS_B2C(ctx->fs, blk) ==
+                     EXT2FS_B2C(ctx->fs, p->previous_block)) &&
+                    (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
+                    (blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
                mark_block_used(ctx, blk);
-       p->num_blocks++;
+               p->num_blocks++;
+       }
        if (blockcnt >= 0)
                p->last_block = blockcnt;
+       p->previous_block = blk;
 mark_dir:
        if (p->is_dir && (blockcnt >= 0)) {
-               pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
-                                                   blk, blockcnt);
+               while (++p->last_db_block < blockcnt) {
+                       pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
+                                                             p->ino, 0,
+                                                             p->last_db_block);
+                       if (pctx->errcode) {
+                               pctx->blk = 0;
+                               pctx->num = p->last_db_block;
+                               goto failed_add_dir_block;
+                       }
+               }
+               pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
+                                                     blk, blockcnt);
                if (pctx->errcode) {
                        pctx->blk = blk;
                        pctx->num = blockcnt;
+               failed_add_dir_block:
                        fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
                        /* Should never get here */
                        ctx->flags |= E2F_FLAG_ABORT;
@@ -1831,15 +2388,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;
@@ -1851,17 +2408,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;
@@ -1870,16 +2427,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;
@@ -1890,7 +2447,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++;
@@ -1900,15 +2457,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;
@@ -1942,22 +2499,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,
@@ -1994,13 +2551,14 @@ static int process_bad_block(ext2_filsys fs,
        return 0;
 }
 
-static void new_table_block(e2fsck_t ctx, blk_t first_block, int group, 
-                           const char *name, int num, blk_t *new_block)
+static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
+                           const char *name, int num, blk64_t *new_block)
 {
        ext2_filsys fs = ctx->fs;
-       blk_t           old_block = *new_block;
-       blk_t           last_block;
-       int             i;
+       dgrp_t          last_grp;
+       blk64_t         old_block = *new_block;
+       blk64_t         last_block;
+       int             i, is_flexbg, flexbg, flexbg_size;
        char            *buf;
        struct problem_context  pctx;
 
@@ -2010,19 +2568,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);
@@ -2033,9 +2616,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);
@@ -2043,7 +2626,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);
@@ -2062,23 +2645,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;
                }
        }
@@ -2092,13 +2682,13 @@ static void handle_fs_bad_blocks(e2fsck_t ctx)
 static void mark_table_blocks(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       blk_t   b;
+       blk64_t b;
        dgrp_t  i;
        int     j;
        struct problem_context pctx;
-       
+
        clear_problem_context(&pctx);
-       
+
        for (i = 0; i < fs->group_desc_count; i++) {
                pctx.group = i;
 
@@ -2107,61 +2697,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
@@ -2173,7 +2764,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;
 
@@ -2216,16 +2807,65 @@ static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
        return 0;
 }
 
-void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
+static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
+                                       blk64_t *ret)
+{
+       e2fsck_t ctx = (e2fsck_t) fs->priv_data;
+       errcode_t       retval;
+       blk64_t         new_block;
+
+       if (ctx->block_found_map) {
+               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);
+                       if (retval)
+                               return retval;
+               }
+
+               retval = ext2fs_new_block2(fs, goal, 0, &new_block);
+               if (retval)
+                       return retval;
+       }
+
+       *ret = new_block;
+       return (0);
+}
+
+static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
+{
+       e2fsck_t ctx = (e2fsck_t) fs->priv_data;
+
+       if (ctx->block_found_map) {
+               if (inuse > 0)
+                       ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
+               else
+                       ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
+       }
+}
+
+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;
                fs->write_inode = pass1_write_inode;
                ctx->stashed_ino = 0;
+               ext2fs_set_alloc_block_callback(fs, e2fsck_get_alloc_block,
+                                               0);
+               ext2fs_set_block_alloc_stats_callback(fs,
+                                                     e2fsck_block_alloc_stats,
+                                                     0);
        } else {
                fs->get_blocks = 0;
                fs->check_directory = 0;