Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / e2fsck / pass1.c
index d2021dd..9a5dac7 100644 (file)
@@ -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);
@@ -74,18 +75,19 @@ 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,
                                    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;
@@ -169,7 +171,7 @@ int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
 {
        unsigned int len;
        int i;
-       blk_t   blocks;
+       blk64_t blocks;
        ext2_extent_handle_t    handle;
        struct ext2_extent_info info;
        struct ext2fs_extent    extent;
@@ -192,7 +194,7 @@ int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
                    (extent.e_lblk != 0) ||
                    (extent.e_len != 1) ||
                    (extent.e_pblk < fs->super->s_first_data_block) ||
-                   (extent.e_pblk >= fs->super->s_blocks_count))
+                   (extent.e_pblk >= ext2fs_blocks_count(fs->super)))
                        goto exit_extent;
                i = 1;
        exit_extent:
@@ -200,19 +202,19 @@ int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
                return i;
        }
 
-       blocks = ext2fs_inode_data_blocks(fs, inode);
+       blocks = ext2fs_inode_data_blocks2(fs, inode);
        if (blocks) {
                if ((inode->i_size >= fs->blocksize) ||
                    (blocks != fs->blocksize >> 9) ||
                    (inode->i_block[0] < fs->super->s_first_data_block) ||
-                   (inode->i_block[0] >= fs->super->s_blocks_count))
+                   (inode->i_block[0] >= ext2fs_blocks_count(fs->super)))
                        return 0;
 
                for (i = 1; i < EXT2_N_BLOCKS; i++)
                        if (inode->i_block[i])
                                return 0;
 
-               if (io_channel_read_blk(fs->io, inode->i_block[0], 1, buf))
+               if (io_channel_read_blk64(fs->io, inode->i_block[0], 1, buf))
                        return 0;
 
                len = strnlen(buf, fs->blocksize);
@@ -256,7 +258,7 @@ 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))
@@ -272,16 +274,15 @@ static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
        struct ext2_super_block *sb = ctx->fs->super;
        struct ext2_inode_large *inode;
        struct ext2_ext_attr_entry *entry;
-       char *start, *end;
+       char *start;
        unsigned int storage_size, remain;
-       int problem = 0;
+       problem_t problem = 0;
 
        inode = (struct ext2_inode_large *) pctx->inode;
        storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
                inode->i_extra_isize;
        start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
                inode->i_extra_isize + sizeof(__u32);
-       end = (char *) inode + EXT2_INODE_SIZE(ctx->fs->super);
        entry = (struct ext2_ext_attr_entry *) start;
 
        /* scan all entry's headers first */
@@ -306,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;
@@ -364,8 +365,8 @@ static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
        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);
+       /* 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
@@ -402,34 +403,77 @@ 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;
+       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 the mode says this is a device file and the i_links_count field
+        * is sane and we have not ruled it out as a device file previously,
+        * we declare it a device file, not a directory.
+        */
        if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
            (inode->i_links_count == 1) && !not_device)
                return;
 
-       old_op = ehandler_operation(_("reading directory block"));
-       retval = ext2fs_read_dir_block(ctx->fs, inode->i_block[0], buf);
+       /* read the first block */
+       ehandler_operation(_("reading directory block"));
+       retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
        ehandler_operation(0);
        if (retval)
                return;
@@ -438,7 +482,7 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
        retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
        if (retval)
                return;
-       if (((dirent->name_len & 0xFF) != 1) ||
+       if ((ext2fs_dirent_name_len(dirent) != 1) ||
            (dirent->name[0] != '.') ||
            (dirent->inode != pctx->ino) ||
            (rec_len < 12) ||
@@ -450,7 +494,7 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
        retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
        if (retval)
                return;
-       if (((dirent->name_len & 0xFF) != 2) ||
+       if ((ext2fs_dirent_name_len(dirent) != 2) ||
            (dirent->name[0] != '.') ||
            (dirent->name[1] != '.') ||
            (rec_len < 12) ||
@@ -496,12 +540,46 @@ extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
                *ret = 0;
 }
 
+static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
+                                           e2fsck_t ctx,
+                                           struct problem_context *pctx)
+{
+       errcode_t retval;
+       struct ext2_inode_large inode;
+
+       /*
+        * Reread inode.  If we don't see checksum error, then this inode
+        * has been fixed elsewhere.
+        */
+       retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
+                                       sizeof(inode));
+       if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
+               return retval;
+       if (!retval)
+               return 0;
+
+       /*
+        * Checksum still doesn't match.  That implies that the inode passes
+        * all the sanity checks, so maybe the checksum is simply corrupt.
+        * See if the user will go for fixing that.
+        */
+       if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
+               return 0;
+
+       retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
+                                        sizeof(inode));
+       if (retval)
+               return retval;
+
+       return 0;
+}
+
 void e2fsck_pass1(e2fsck_t ctx)
 {
        int     i;
        __u64   max_sizes;
        ext2_filsys fs = ctx->fs;
-       ext2_ino_t      ino;
+       ext2_ino_t      ino = 0;
        struct ext2_inode *inode;
        ext2_inode_scan scan;
        char            *block_buf;
@@ -513,9 +591,11 @@ void e2fsck_pass1(e2fsck_t ctx)
        struct          scan_callback_struct scan_struct;
        struct ext2_super_block *sb = ctx->fs->super;
        const char      *old_op;
+       unsigned int    save_type;
        int             imagic_fs, extent_fs;
        int             busted_fs_time = 0;
        int             inode_size;
+       int             failed_csum = 0;
 
        init_resource_track(&rtrack, ctx->fs->io);
        clear_problem_context(&pctx);
@@ -539,7 +619,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
                max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
                max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
-               max_sizes = (max_sizes * (1UL << i)) - 1;
+               max_sizes = (max_sizes * (1UL << i));
                ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
        }
 #undef EXT2_BPP
@@ -550,32 +630,38 @@ void e2fsck_pass1(e2fsck_t ctx)
        /*
         * Allocate bitmaps structures
         */
-       pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
-                                             &ctx->inode_used_map);
+       pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
+                                                   EXT2FS_BMAP64_RBTREE,
+                                                   "inode_used_map",
+                                                   &ctx->inode_used_map);
        if (pctx.errcode) {
                pctx.num = 1;
                fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
-       pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
-                               _("directory inode map"), &ctx->inode_dir_map);
+       pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
+                       _("directory inode map"),
+                       EXT2FS_BMAP64_AUTODIR,
+                       "inode_dir_map", &ctx->inode_dir_map);
        if (pctx.errcode) {
                pctx.num = 2;
                fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
-       pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
-                       _("regular file inode map"), &ctx->inode_reg_map);
+       pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
+                       _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
+                       "inode_reg_map", &ctx->inode_reg_map);
        if (pctx.errcode) {
                pctx.num = 6;
                fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
-       pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
-                                             &ctx->block_found_map);
+       pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
+                       _("in-use block map"), EXT2FS_BMAP64_RBTREE,
+                       "block_found_map", &ctx->block_found_map);
        if (pctx.errcode) {
                pctx.num = 1;
                fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
@@ -583,9 +669,14 @@ void e2fsck_pass1(e2fsck_t ctx)
                return;
        }
        e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
-       if (!ctx->inode_link_info)
+       if (!ctx->inode_link_info) {
+               e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
+                                      "inode_link_info", &save_type);
                pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
                                                     &ctx->inode_link_info);
+               fs->default_bitmap_type = save_type;
+       }
+
        if (pctx.errcode) {
                fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
@@ -625,9 +716,18 @@ void e2fsck_pass1(e2fsck_t ctx)
        }
 
        mark_table_blocks(ctx);
+       pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
+                                               &ctx->block_found_map);
+       if (pctx.errcode) {
+               fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
+               ctx->flags |= E2F_FLAG_ABORT;
+               ext2fs_free_mem(&inode);
+               return;
+       }
        block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
                                                    "block interate buffer");
-       e2fsck_use_inode_shortcuts(ctx, 1);
+       if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
+               e2fsck_use_inode_shortcuts(ctx, 1);
        old_op = ehandler_operation(_("opening inode scan"));
        pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
                                              &scan);
@@ -651,7 +751,20 @@ void e2fsck_pass1(e2fsck_t ctx)
            (fs->super->s_mtime < fs->super->s_inodes_count))
                busted_fs_time = 1;
 
+       if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
+           fs->super->s_mmp_block > fs->super->s_first_data_block &&
+           fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
+               ext2fs_mark_block_bitmap2(ctx->block_found_map,
+                                         fs->super->s_mmp_block);
+
+       /* Set up ctx->lost_and_found if possible */
+       (void) e2fsck_get_lost_and_found(ctx, 0);
+
        while (1) {
+               if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
+                       if (e2fsck_mmp_update(fs))
+                               fatal_error(ctx, 0);
+               }
                old_op = ehandler_operation(_("getting next inode from scan"));
                pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
                                                          inode, inode_size);
@@ -661,11 +774,12 @@ void e2fsck_pass1(e2fsck_t ctx)
                if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
                        if (!ctx->inode_bb_map)
                                alloc_bb_map(ctx);
-                       ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
-                       ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
+                       ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
+                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
                        continue;
                }
-               if (pctx.errcode) {
+               if (pctx.errcode &&
+                   pctx.errcode != EXT2_ET_INODE_CSUM_INVALID) {
                        fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
                        ctx->flags |= E2F_FLAG_ABORT;
                        return;
@@ -675,6 +789,14 @@ void e2fsck_pass1(e2fsck_t ctx)
                pctx.ino = ino;
                pctx.inode = inode;
                ctx->stashed_ino = ino;
+
+               /* Clear corrupt inode? */
+               if (pctx.errcode == EXT2_ET_INODE_CSUM_INVALID) {
+                       if (fix_problem(ctx, PR_1_INODE_CSUM_INVALID, &pctx))
+                               goto clear_inode;
+                       failed_csum = 1;
+               }
+
                if (inode->i_links_count) {
                        pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
                                           ino, inode->i_links_count);
@@ -714,7 +836,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                        clear_inode:
                                e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
                                if (ino == EXT2_BAD_INO)
-                                       ext2fs_mark_inode_bitmap(ctx->inode_used_map,
+                                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
                                                                 ino);
                                continue;
                        }
@@ -758,6 +880,14 @@ void e2fsck_pass1(e2fsck_t ctx)
                if (ino == EXT2_BAD_INO) {
                        struct process_block_struct pb;
 
+                       if ((inode->i_mode || inode->i_uid || inode->i_gid ||
+                            inode->i_links_count || inode->i_file_acl) &&
+                           fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
+                               memset(inode, 0, sizeof(struct ext2_inode));
+                               e2fsck_write_inode(ctx, ino, inode,
+                                                  "clear bad inode");
+                       }
+
                        pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
                                                          &pb.fs_meta_blocks);
                        if (pctx.errcode) {
@@ -775,7 +905,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                        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) {
@@ -788,7 +918,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                                ctx->flags |= E2F_FLAG_ABORT;
                                return;
                        }
-                       ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
+                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
                        clear_problem_context(&pctx);
                        continue;
                } else if (ino == EXT2_ROOT_INO) {
@@ -818,7 +948,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                                }
                        }
                } else if (ino == EXT2_JOURNAL_INO) {
-                       ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
+                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
                        if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
                                if (!LINUX_S_ISREG(inode->i_mode) &&
                                    fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
@@ -830,7 +960,7 @@ 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)) {
@@ -840,10 +970,37 @@ void e2fsck_pass1(e2fsck_t ctx)
                                e2fsck_write_inode_full(ctx, ino, inode,
                                                        inode_size, "pass1");
                        }
+               } else if ((ino == EXT4_USR_QUOTA_INO) ||
+                          (ino == EXT4_GRP_QUOTA_INO)) {
+                       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
+                       if ((fs->super->s_feature_ro_compat &
+                                       EXT4_FEATURE_RO_COMPAT_QUOTA) &&
+                           ((fs->super->s_usr_quota_inum == ino) ||
+                            (fs->super->s_grp_quota_inum == ino))) {
+                               if (!LINUX_S_ISREG(inode->i_mode) &&
+                                   fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
+                                                       &pctx)) {
+                                       inode->i_mode = LINUX_S_IFREG;
+                                       e2fsck_write_inode(ctx, ino, inode,
+                                                       "pass1");
+                               }
+                               check_blocks(ctx, &pctx, block_buf);
+                               continue;
+                       }
+                       if ((inode->i_links_count ||
+                            inode->i_blocks || inode->i_block[0]) &&
+                           fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
+                                       &pctx)) {
+                               memset(inode, 0, inode_size);
+                               ext2fs_icount_store(ctx->inode_link_info,
+                                                   ino, 0);
+                               e2fsck_write_inode_full(ctx, ino, inode,
+                                                       inode_size, "pass1");
+                       }
                } else if (ino < EXT2_FIRST_INODE(fs->super)) {
-                       int     problem = 0;
+                       problem_t 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;
@@ -865,6 +1022,7 @@ 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
@@ -924,7 +1082,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                        }
                }
 
-               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;
@@ -950,7 +1108,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                        if (imagic_fs) {
                                if (!ctx->inode_imagic_map)
                                        alloc_imagic_map(ctx);
-                               ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
+                               ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
                                                         ino);
                        } else {
                                if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
@@ -965,23 +1123,23 @@ void e2fsck_pass1(e2fsck_t ctx)
                check_is_really_dir(ctx, &pctx, block_buf);
 
                /*
-                * ext2fs_inode_has_valid_blocks does not actually look
+                * ext2fs_inode_has_valid_blocks2 does not actually look
                 * at i_block[] values, so not endian-sensitive here.
                 */
                if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
                    LINUX_S_ISLNK(inode->i_mode) &&
-                   !ext2fs_inode_has_valid_blocks(inode) &&
+                   !ext2fs_inode_has_valid_blocks2(fs, inode) &&
                    fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
                        inode->i_flags &= ~EXT4_EXTENTS_FL;
                        e2fsck_write_inode(ctx, ino, inode, "pass1");
                }
 
                if (LINUX_S_ISDIR(inode->i_mode)) {
-                       ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
+                       ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
                        e2fsck_add_dir_info(ctx, ino, 0);
                        ctx->fs_directory_count++;
                } else if (LINUX_S_ISREG (inode->i_mode)) {
-                       ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
+                       ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
                        ctx->fs_regular_count++;
                } else if (LINUX_S_ISCHR (inode->i_mode) &&
                           e2fsck_pass1_check_device_inode(fs, inode)) {
@@ -1028,13 +1186,27 @@ void e2fsck_pass1(e2fsck_t ctx)
                    (inode->i_block[EXT2_IND_BLOCK] ||
                     inode->i_block[EXT2_DIND_BLOCK] ||
                     inode->i_block[EXT2_TIND_BLOCK] ||
-                    inode->i_file_acl)) {
+                    ext2fs_file_acl_block(fs, inode))) {
                        inodes_to_process[process_inode_count].ino = ino;
                        inodes_to_process[process_inode_count].inode = *inode;
                        process_inode_count++;
                } else
                        check_blocks(ctx, &pctx, block_buf);
 
+               /*
+                * If the inode failed the checksum and the user didn't
+                * clear the inode, test the checksum again -- if it still
+                * fails, ask the user if the checksum should be corrected.
+                */
+               if (failed_csum) {
+                       pctx.errcode = recheck_bad_inode_checksum(fs, ino, ctx,
+                                                                 &pctx);
+                       if (pctx.errcode) {
+                               ctx->flags |= E2F_FLAG_ABORT;
+                               return;
+                       }
+               }
+
                if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
                        return;
 
@@ -1082,16 +1254,20 @@ 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;
        }
@@ -1122,6 +1298,12 @@ endit:
        ext2fs_free_mem(&block_buf);
        ext2fs_free_mem(&inode);
 
+       /*
+        * The l+f inode may have been cleared, so zap it now and
+        * later passes will recalculate it if necessary
+        */
+       ctx->lost_and_found = 0;
+
        print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
 }
 
@@ -1206,7 +1388,13 @@ static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
        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;
@@ -1222,8 +1410,9 @@ 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);
@@ -1232,7 +1421,7 @@ static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
                        return;
                }
        }
-       ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
+       ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
 }
 
 
@@ -1244,9 +1433,9 @@ 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);
@@ -1264,9 +1453,9 @@ 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);
@@ -1283,17 +1472,18 @@ 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,
@@ -1303,12 +1493,22 @@ static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
                                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
@@ -1322,7 +1522,7 @@ static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
        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;
 
@@ -1333,7 +1533,8 @@ static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
                if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
                        break;
                pctx.blk = blk;
-               pctx.errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
+               pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
+                                                    pctx.ino);
                if (pctx.errcode) {
                        fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
                        return;
@@ -1344,10 +1545,12 @@ static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
                pctx.num = should_be;
                if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
                        header->h_refcount = should_be;
-                       pctx.errcode = ext2fs_write_ext_attr(fs, blk,
-                                                            block_buf);
+                       pctx.errcode = ext2fs_write_ext_attr3(fs, blk,
+                                                            block_buf,
+                                                            pctx.ino);
                        if (pctx.errcode) {
-                               fix_problem(ctx, PR_1_EXTATTR_WRITE, &pctx);
+                               fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
+                                           &pctx);
                                continue;
                        }
                }
@@ -1363,14 +1566,15 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
        ext2_filsys fs = ctx->fs;
        ext2_ino_t      ino = pctx->ino;
        struct ext2_inode *inode = pctx->inode;
-       blk_t           blk;
+       blk64_t         blk;
        char *          end;
        struct ext2_ext_attr_header *header;
        struct ext2_ext_attr_entry *entry;
        int             count;
        region_t        region = 0;
+       int             failed_csum = 0;
 
-       blk = inode->i_file_acl;
+       blk = ext2fs_file_acl_block(fs, inode);
        if (blk == 0)
                return 0;
 
@@ -1383,16 +1587,17 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
         */
        if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
            (blk < fs->super->s_first_data_block) ||
-           (blk >= fs->super->s_blocks_count)) {
+           (blk >= ext2fs_blocks_count(fs->super))) {
                mark_inode_bad(ctx, ino);
                return 0;
        }
 
        /* If ea bitmap hasn't been allocated, create it */
        if (!ctx->block_ea_map) {
-               pctx->errcode = ext2fs_allocate_block_bitmap(fs,
-                                                     _("ext attr block map"),
-                                                     &ctx->block_ea_map);
+               pctx->errcode = e2fsck_allocate_block_bitmap(fs,
+                                       _("ext attr block map"),
+                                       EXT2FS_BMAP64_RBTREE, "block_ea_map",
+                                       &ctx->block_ea_map);
                if (pctx->errcode) {
                        pctx->num = 2;
                        fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
@@ -1418,7 +1623,7 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
 #endif
 
        /* Have we seen this EA block before? */
-       if (ext2fs_fast_test_block_bitmap(ctx->block_ea_map, blk)) {
+       if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
                if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
                        return 1;
                /* Ooops, this EA was referenced more than it stated */
@@ -1441,11 +1646,16 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
         * validate it
         */
        pctx->blk = blk;
-       pctx->errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
+       pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
+       if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
+               if (fix_problem(ctx, PR_1_EA_BLOCK_CSUM_INVALID, pctx))
+                       goto clear_extattr;
+               failed_csum = 1;
+       }
        if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
                goto clear_extattr;
        header = (struct ext2_ext_attr_header *) block_buf;
-       pctx->blk = inode->i_file_acl;
+       pctx->blk = ext2fs_file_acl_block(fs, inode);
        if (((ctx->ext_attr_ver == 1) &&
             (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
            ((ctx->ext_attr_ver == 2) &&
@@ -1461,7 +1671,7 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
 
        region = region_create(0, fs->blocksize);
        if (!region) {
-               fix_problem(ctx, PR_1_EA_ALLOC_REGION, pctx);
+               fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return 0;
        }
@@ -1523,17 +1733,29 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
        }
        region_free(region);
 
+       /*
+        * We only get here if there was no other errors that were fixed.
+        * If there was a checksum fail, ask to correct it.
+        */
+       if (failed_csum &&
+           fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
+               pctx->errcode = ext2fs_write_ext_attr3(fs, blk, block_buf,
+                                                      pctx->ino);
+               if (pctx->errcode)
+                       return 0;
+       }
+
        count = header->h_refcount - 1;
        if (count)
                ea_refcount_store(ctx->refcount, blk, count);
        mark_block_used(ctx, blk);
-       ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk);
+       ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
        return 1;
 
 clear_extattr:
        if (region)
                region_free(region);
-       inode->i_file_acl = 0;
+       ext2fs_file_acl_block_set(fs, inode, 0);
        e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
        return 0;
 }
@@ -1546,7 +1768,7 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
        struct ext2_dx_root_info        *root;
        ext2_filsys                     fs = ctx->fs;
        errcode_t                       retval;
-       blk_t                           blk;
+       blk64_t                         blk;
 
        if ((!LINUX_S_ISDIR(inode->i_mode) &&
             fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
@@ -1554,19 +1776,19 @@ static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
             fix_problem(ctx, PR_1_HTREE_SET, pctx)))
                return 1;
 
-       pctx->errcode = ext2fs_bmap(fs, ino, inode, 0, 0, 0, &blk);
+       pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
 
        if ((pctx->errcode) ||
            (blk == 0) ||
            (blk < fs->super->s_first_data_block) ||
-           (blk >= fs->super->s_blocks_count)) {
+           (blk >= ext2fs_blocks_count(fs->super))) {
                if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
                        return 1;
                else
                        return 0;
        }
 
-       retval = io_channel_read_blk(fs->io, blk, 1, block_buf);
+       retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
        if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
                return 1;
 
@@ -1605,12 +1827,12 @@ void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
        ext2fs_icount_store(ctx->inode_link_info, ino, 0);
        inode->i_dtime = ctx->now;
 
-       ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
-       ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
+       ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
+       ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
        if (ctx->inode_reg_map)
-               ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
+               ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
        if (ctx->inode_bad_map)
-               ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
+               ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
 
        /*
         * If the inode was partially accounted for before processing
@@ -1618,21 +1840,26 @@ void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
         */
        ctx->flags |= restart_flag;
 
+       if (ino == EXT2_BAD_INO)
+               memset(inode, 0, sizeof(struct ext2_inode));
+
        e2fsck_write_inode(ctx, ino, inode, source);
 }
 
 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                             struct process_block_struct *pb,
-                            blk64_t start_block,
+                            blk64_t start_block, blk64_t end_block,
+                            blk64_t eof_block,
                             ext2_extent_handle_t ehandle)
 {
        struct ext2fs_extent    extent;
-       blk_t                   blk;
+       blk64_t                 blk, last_lblk;
        e2_blkcnt_t             blockcnt;
        unsigned int            i;
        int                     is_dir, is_leaf;
-       errcode_t               problem;
+       problem_t               problem;
        struct ext2_extent_info info;
+       int                     failed_csum;
 
        pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
        if (pctx->errcode)
@@ -1640,33 +1867,74 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
 
        pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
                                          &extent);
-       while (!pctx->errcode && info.num_entries-- > 0) {
+       while ((pctx->errcode == 0 ||
+               pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
+              info.num_entries-- > 0) {
+               failed_csum = 0;
                is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
                is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
+               last_lblk = extent.e_lblk + extent.e_len - 1;
 
                problem = 0;
-               if (extent.e_pblk < ctx->fs->super->s_first_data_block ||
-                   extent.e_pblk >= ctx->fs->super->s_blocks_count)
+               /* Ask to clear a corrupt extent block */
+               if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) {
+                       pctx->blk = extent.e_pblk;
+                       pctx->blk2 = extent.e_lblk;
+                       pctx->num = extent.e_len;
+                       problem = PR_1_EXTENT_CSUM_INVALID;
+                       if (fix_problem(ctx, problem, pctx))
+                               goto fix_problem_now;
+                       failed_csum = 1;
+               }
+
+               if (extent.e_pblk == 0 ||
+                   extent.e_pblk < ctx->fs->super->s_first_data_block ||
+                   extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
                        problem = PR_1_EXTENT_BAD_START_BLK;
                else if (extent.e_lblk < start_block)
                        problem = PR_1_OUT_OF_ORDER_EXTENTS;
+               else if ((end_block && last_lblk > end_block) &&
+                        (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
+                               last_lblk > eof_block)))
+                       problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
+               else if (is_leaf && extent.e_len == 0)
+                       problem = PR_1_EXTENT_LENGTH_ZERO;
                else if (is_leaf &&
                         (extent.e_pblk + extent.e_len) >
-                        ctx->fs->super->s_blocks_count)
+                        ext2fs_blocks_count(ctx->fs->super))
                        problem = PR_1_EXTENT_ENDS_BEYOND;
+               else if (is_leaf && is_dir &&
+                        ((extent.e_lblk + extent.e_len) >
+                         (1 << (21 - ctx->fs->super->s_log_block_size))))
+                       problem = PR_1_TOOBIG_DIR;
+
+               /* Corrupt but passes checks?  Ask to fix checksum. */
+               if (failed_csum) {
+                       pctx->blk = extent.e_pblk;
+                       pctx->blk2 = extent.e_lblk;
+                       pctx->num = extent.e_len;
+                       problem = 0;
+                       if (fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID,
+                                       pctx))
+                               ext2fs_extent_replace(ehandle, 0, &extent);
+               }
 
                if (problem) {
-               report_problem:
+report_problem:
                        pctx->blk = extent.e_pblk;
                        pctx->blk2 = extent.e_lblk;
                        pctx->num = extent.e_len;
+                       pctx->blkcount = extent.e_lblk + extent.e_len;
                        if (fix_problem(ctx, problem, pctx)) {
+fix_problem_now:
+                               e2fsck_read_bitmaps(ctx);
                                pctx->errcode =
                                        ext2fs_extent_delete(ehandle, 0);
                                if (pctx->errcode) {
                                        pctx->str = "ext2fs_extent_delete";
                                        return;
                                }
+                               ext2fs_extent_fix_parents(ehandle);
                                pctx->errcode = ext2fs_extent_get(ehandle,
                                                                  EXT2_EXTENT_CURRENT,
                                                                  &extent);
@@ -1680,17 +1948,35 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                }
 
                if (!is_leaf) {
+                       blk64_t lblk = extent.e_lblk;
+
                        blk = extent.e_pblk;
                        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)
+                               if (pctx->errcode ==
+                                       EXT2_ET_EXTENT_HEADER_BAD ||
+                                   pctx->errcode ==
+                                       EXT2_ET_EXTENT_CSUM_INVALID)
                                        goto report_problem;
                                return;
                        }
-                       scan_extent_node(ctx, pctx, pb, extent.e_lblk, ehandle);
+                       /* The next extent should match this index's logical start */
+                       if (extent.e_lblk != lblk) {
+                               struct ext2_extent_info e_info;
+
+                               ext2fs_extent_get_info(ehandle, &e_info);
+                               pctx->blk = lblk;
+                               pctx->blk2 = extent.e_lblk;
+                               pctx->num = e_info.curr_level - 1;
+                               problem = PR_1_EXTENT_INDEX_START_INVALID;
+                               if (fix_problem(ctx, problem, pctx))
+                                       ext2fs_extent_fix_parents(ehandle);
+                       }
+                       scan_extent_node(ctx, pctx, pb, extent.e_lblk,
+                                        last_lblk, eof_block, ehandle);
                        if (pctx->errcode)
                                return;
                        pctx->errcode = ext2fs_extent_get(ehandle,
@@ -1725,16 +2011,42 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                        }
                        pb->fragmented = 1;
                }
+               while (is_dir && (++pb->last_db_block <
+                                 (e2_blkcnt_t) extent.e_lblk)) {
+                       pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
+                                                             pb->ino, 0,
+                                                             pb->last_db_block);
+                       if (pctx->errcode) {
+                               pctx->blk = 0;
+                               pctx->num = pb->last_db_block;
+                               goto failed_add_dir_block;
+                       }
+               }
+               if (!ctx->fs->cluster_ratio_bits) {
+                       mark_blocks_used(ctx, extent.e_pblk, extent.e_len);
+                       pb->num_blocks += extent.e_len;
+               }
                for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
                     i < extent.e_len;
                     blk++, blockcnt++, i++) {
-                       mark_block_used(ctx, blk);
+                       if (ctx->fs->cluster_ratio_bits &&
+                           !(pb->previous_block &&
+                             (EXT2FS_B2C(ctx->fs, blk) ==
+                              EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
+                             (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
+                             ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
+                               mark_block_used(ctx, blk);
+                               pb->num_blocks++;
+                       }
+
+                       pb->previous_block = blk;
 
                        if (is_dir) {
-                               pctx->errcode = ext2fs_add_dir_block(ctx->fs->dblist, pctx->ino, blk, blockcnt);
+                               pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
                                if (pctx->errcode) {
                                        pctx->blk = blk;
                                        pctx->num = blockcnt;
+                               failed_add_dir_block:
                                        fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
                                        /* Should never get here */
                                        ctx->flags |= E2F_FLAG_ABORT;
@@ -1742,11 +2054,13 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
                                }
                        }
                }
-               pb->num_blocks += extent.e_len;
+               if (is_dir && extent.e_len > 0)
+                       pb->last_db_block = blockcnt - 1;
                pb->previous_block = extent.e_pblk + extent.e_len - 1;
-               start_block = extent.e_lblk + extent.e_len - 1;
-               if (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
-                       pb->last_block = start_block;
+               start_block = pb->last_block = last_lblk;
+               if (is_leaf && !is_dir &&
+                   !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
+                       pb->last_init_lblock = last_lblk;
        next:
                pctx->errcode = ext2fs_extent_get(ehandle,
                                                  EXT2_EXTENT_NEXT_SIB,
@@ -1765,6 +2079,7 @@ static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
        ext2_filsys             fs = ctx->fs;
        ext2_ino_t              ino = pctx->ino;
        errcode_t               retval;
+       blk64_t                 eof_lblk;
 
        pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
        if (pctx->errcode) {
@@ -1782,7 +2097,9 @@ static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
                ctx->extent_depth_count[info.max_depth]++;
        }
 
-       scan_extent_node(ctx, pctx, pb, 0, ehandle);
+       eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
+               EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
+       scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle);
        if (pctx->errcode &&
            fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
                pb->num_blocks = 0;
@@ -1794,15 +2111,6 @@ static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
        ext2fs_extent_free(ehandle);
 }
 
-static blk64_t ext2fs_inode_i_blocks(ext2_filsys fs,
-                                    struct ext2_inode *inode)
-{
-       return (inode->i_blocks |
-               (fs->super->s_feature_ro_compat & 
-                EXT4_FEATURE_RO_COMPAT_HUGE_FILE ?
-                (__u64)inode->osd2.linux2.l_i_blocks_hi << 32 : 0));
-}
-
 /*
  * This subroutine is called on each inode to account for all of the
  * blocks used by that inode.
@@ -1814,7 +2122,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        struct process_block_struct pb;
        ext2_ino_t      ino = pctx->ino;
        struct ext2_inode *inode = pctx->inode;
-       int             bad_size = 0;
+       unsigned        bad_size = 0;
        int             dirty_inode = 0;
        int             extent_fs;
        __u64           size;
@@ -1822,6 +2130,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        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;
@@ -1852,19 +2161,26 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                }
        }
 
-       if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf)) {
+       if (ext2fs_file_acl_block(fs, inode) &&
+           check_ext_attr(ctx, pctx, block_buf)) {
                if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
                        goto out;
                pb.num_blocks++;
        }
 
-       if (ext2fs_inode_has_valid_blocks(inode)) {
+       if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
                if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
                        check_blocks_extents(ctx, pctx, &pb);
-               else
-                       pctx->errcode = ext2fs_block_iterate2(fs, ino,
+               else {
+                       pctx->errcode = ext2fs_block_iterate3(fs, ino,
                                                pb.is_dir ? BLOCK_FLAG_HOLE : 0,
                                                block_buf, process_block, &pb);
+                       /*
+                        * We do not have uninitialized extents in non extent
+                        * files.
+                        */
+                       pb.last_init_lblock = pb.last_block;
+               }
        }
        end_problem_latch(ctx, PR_LATCH_BLOCK);
        end_problem_latch(ctx, PR_LATCH_TOOBIG);
@@ -1905,13 +2221,20 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                }
        }
 
+       if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
+               quota_data_add(ctx->qctx, inode, ino,
+                              pb.num_blocks * fs->blocksize);
+               quota_data_inodes(ctx->qctx, inode, ino, +1);
+       }
+
        if (!(fs->super->s_feature_ro_compat &
              EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
            !(inode->i_flags & EXT4_HUGE_FILE_FL))
                pb.num_blocks *= (fs->blocksize / 512);
+       pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
 #if 0
-       printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
-              ino, inode->i_size, pb.last_block, inode->i_blocks,
+       printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
+              ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
               pb.num_blocks);
 #endif
        if (pb.is_dir) {
@@ -1929,11 +2252,12 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                e2_blkcnt_t blkpg = ctx->blocks_per_page;
 
                size = EXT2_I_SIZE(inode);
-               if ((pb.last_block >= 0) &&
+               if ((pb.last_init_lblock >= 0) &&
                    /* allow allocated blocks to end of PAGE_SIZE */
-                   (size < (__u64)pb.last_block * fs->blocksize) &&
-                   (pb.last_block / blkpg * blkpg != pb.last_block ||
-                    size < (__u64)(pb.last_block & ~(blkpg-1)) *fs->blocksize))
+                   (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
+                   (pb.last_init_lblock / blkpg * blkpg != pb.last_init_lblock ||
+                    size < (__u64)(pb.last_init_lblock & ~(blkpg-1)) *
+                    fs->blocksize))
                        bad_size = 3;
                else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
                         size > ext2_max_sizes[fs->super->s_log_block_size])
@@ -1957,8 +2281,7 @@ 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 != ext2fs_inode_i_blocks(fs, inode)) ||
            ((fs->super->s_feature_ro_compat &
@@ -1968,16 +2291,17 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                pctx->num = pb.num_blocks;
                if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
                        inode->i_blocks = pb.num_blocks;
-                       inode->osd2.linux2.l_i_blocks_hi = 0;
+                       inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
                        dirty_inode++;
                }
                pctx->num = 0;
        }
 
        if (ctx->dirs_to_hash && pb.is_dir &&
+           !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
            !(inode->i_flags & EXT2_INDEX_FL) &&
            ((inode->i_size / fs->blocksize) >= 3))
-               ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
+               e2fsck_rehash_dir_later(ctx, ino);
 
 out:
        if (dirty_inode)
@@ -1989,9 +2313,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];
 
@@ -2000,8 +2324,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++) {
@@ -2015,16 +2339,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);
@@ -2040,17 +2364,17 @@ static char *describe_illegal_block(ext2_filsys fs, blk_t block)
  * This is a helper function for check_blocks().
  */
 static int process_block(ext2_filsys fs,
-                 blk_t *block_nr,
+                 blk64_t       *block_nr,
                  e2_blkcnt_t blockcnt,
-                 blk_t ref_block EXT2FS_ATTR((unused)),
+                 blk64_t ref_block EXT2FS_ATTR((unused)),
                  int ref_offset EXT2FS_ATTR((unused)),
                  void *priv_data)
 {
        struct process_block_struct *p;
        struct problem_context *pctx;
-       blk_t   blk = *block_nr;
+       blk64_t blk = *block_nr;
        int     ret_code = 0;
-       int     problem = 0;
+       problem_t       problem = 0;
        e2fsck_t        ctx;
 
        p = (struct process_block_struct *) priv_data;
@@ -2112,7 +2436,6 @@ static int process_block(ext2_filsys fs,
                        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;
@@ -2122,7 +2445,7 @@ static int process_block(ext2_filsys fs,
                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) {
@@ -2158,25 +2481,33 @@ static int process_block(ext2_filsys fs,
                 */
                if (blockcnt == BLOCK_COUNT_DIND)
                        mark_block_used(ctx, blk);
-       } else
+               p->num_blocks++;
+       } else if (!(ctx->fs->cluster_ratio_bits &&
+                    p->previous_block &&
+                    (EXT2FS_B2C(ctx->fs, blk) ==
+                     EXT2FS_B2C(ctx->fs, p->previous_block)) &&
+                    (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
+                    ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
                mark_block_used(ctx, blk);
-       p->num_blocks++;
+               p->num_blocks++;
+       }
        if (blockcnt >= 0)
                p->last_block = blockcnt;
+       p->previous_block = blk;
 mark_dir:
        if (p->is_dir && (blockcnt >= 0)) {
                while (++p->last_db_block < blockcnt) {
-                       pctx->errcode = ext2fs_add_dir_block(fs->dblist,
-                                                            p->ino, 0,
-                                                            p->last_db_block);
+                       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_block(fs->dblist, p->ino,
-                                                   blk, blockcnt);
+               pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
+                                                     blk, blockcnt);
                if (pctx->errcode) {
                        pctx->blk = blk;
                        pctx->num = blockcnt;
@@ -2191,15 +2522,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;
@@ -2221,7 +2552,7 @@ static int process_bad_block(ext2_filsys fs,
        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;
@@ -2230,13 +2561,13 @@ 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,
@@ -2260,8 +2591,8 @@ 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;
        }
        /*
@@ -2302,22 +2633,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,
@@ -2354,14 +2685,16 @@ static int process_bad_block(ext2_filsys fs,
        return 0;
 }
 
-static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
-                           const char *name, int num, blk_t *new_block)
+static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
+                           const char *name, int num, blk64_t *new_block)
 {
        ext2_filsys fs = ctx->fs;
        dgrp_t          last_grp;
-       blk_t           old_block = *new_block;
-       blk_t           last_block;
-       int             i, is_flexbg, flexbg, flexbg_size;
+       blk64_t         old_block = *new_block;
+       blk64_t         last_block;
+       dgrp_t          flexbg;
+       unsigned        flexbg_size;
+       int             i, is_flexbg;
        char            *buf;
        struct problem_context  pctx;
 
@@ -2381,21 +2714,21 @@ static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
        if (is_flexbg) {
                flexbg_size = 1 << fs->super->s_log_groups_per_flex;
                flexbg = group / flexbg_size;
-               first_block = ext2fs_group_first_block(fs,
-                                                      flexbg_size * flexbg);
+               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_block(fs, last_grp);
+               last_block = ext2fs_group_last_block2(fs, last_grp);
        } else
-               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);
+               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_blocks(fs,
+               pctx.errcode = ext2fs_get_free_blocks2(fs,
                                fs->super->s_first_data_block,
-                               fs->super->s_blocks_count,
+                               ext2fs_blocks_count(fs->super),
                                num, ctx->block_found_map, new_block);
        if (pctx.errcode) {
                pctx.num = num;
@@ -2419,9 +2752,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);
@@ -2429,7 +2762,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);
@@ -2448,23 +2781,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);
+                                       &new_blk);
+                       ext2fs_inode_table_loc_set(fs, i, new_blk);
                        ctx->flags |= E2F_FLAG_RESTART;
                }
        }
@@ -2478,9 +2818,9 @@ static void handle_fs_bad_blocks(e2fsck_t ctx)
 static void mark_table_blocks(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       blk_t   b;
+       blk64_t b;
        dgrp_t  i;
-       int     j;
+       unsigned int    j;
        struct problem_context pctx;
 
        clear_problem_context(&pctx);
@@ -2493,11 +2833,11 @@ 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 (!ctx->invalid_inode_table_flag[i] &&
@@ -2507,7 +2847,7 @@ static void mark_table_blocks(e2fsck_t ctx)
                                                ctx->invalid_bitmaps++;
                                        }
                                } else {
-                                   ext2fs_mark_block_bitmap(ctx->block_found_map,
+                                   ext2fs_mark_block_bitmap2(ctx->block_found_map,
                                                             b);
                                }
                        }
@@ -2516,34 +2856,34 @@ static void mark_table_blocks(e2fsck_t ctx)
                /*
                 * 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
                 */
-               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));
                        }
                }
        }
@@ -2608,13 +2948,17 @@ static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
 {
        e2fsck_t ctx = (e2fsck_t) fs->priv_data;
        errcode_t       retval;
-       blk_t           new_block;
+       blk64_t         new_block;
 
        if (ctx->block_found_map) {
-               retval = ext2fs_new_block(fs, (blk_t) goal,
-                                         ctx->block_found_map, &new_block);
+               retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
+                                          &new_block);
                if (retval)
                        return retval;
+               if (fs->block_map) {
+                       ext2fs_mark_block_bitmap2(fs->block_map, new_block);
+                       ext2fs_mark_bb_dirty(fs);
+               }
        } else {
                if (!fs->block_map) {
                        retval = ext2fs_read_block_bitmap(fs);
@@ -2622,7 +2966,7 @@ static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
                                return retval;
                }
 
-               retval = ext2fs_new_block(fs, (blk_t) goal, 0, &new_block);
+               retval = ext2fs_new_block2(fs, goal, 0, &new_block);
                if (retval)
                        return retval;
        }
@@ -2637,19 +2981,17 @@ static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
 
        if (ctx->block_found_map) {
                if (inuse > 0)
-                       ext2fs_mark_block_bitmap(ctx->block_found_map,
-                                                (blk_t) blk);
+                       ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
                else
-                       ext2fs_unmark_block_bitmap(ctx->block_found_map,
-                                                  (blk_t) blk);
+                       ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
        }
 }
 
-void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
+void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
 {
        ext2_filsys fs = ctx->fs;
 
-       if (bool) {
+       if (use_shortcuts) {
                fs->get_blocks = pass1_get_blocks;
                fs->check_directory = pass1_check_directory;
                fs->read_inode = pass1_read_inode;