Whamcloud - gitweb
Fix encoding for rec_len in directories for >= 64k blocksize file systems
[tools/e2fsprogs.git] / e2fsck / pass1.c
index e1aa029..518c2ff 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:
  *
@@ -56,7 +56,7 @@
 #endif
 
 static int process_block(ext2_filsys fs, blk_t *blocknr,
-                        e2_blkcnt_t blockcnt, blk_t ref_blk, 
+                        e2_blkcnt_t blockcnt, blk_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,
@@ -72,7 +72,7 @@ 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); */
 
@@ -126,9 +126,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 +152,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,17 +163,42 @@ 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;
+       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;
 
+       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 >= fs->super->s_blocks_count))
+                       goto exit_extent;
+               i = 1;
+       exit_extent:
+               ext2fs_extent_free(handle);
+               return i;
+       }
+
        blocks = ext2fs_inode_data_blocks(fs, inode);
        if (blocks) {
                if ((inode->i_size >= fs->blocksize) ||
@@ -229,18 +254,18 @@ static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
 {
        struct ext2_inode *inode = pctx->inode;
-       
+
        if ((inode->i_size == 0) && (inode->i_size_high == 0))
                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;
@@ -261,13 +286,14 @@ static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
        /* scan all entry's headers first */
 
        /* take finish entry 0UL into account */
-       remain = storage_size - sizeof(__u32); 
+       remain = storage_size - sizeof(__u32);
 
        while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
+               __u32 hash;
 
                /* header eats this space */
                remain -= sizeof(struct ext2_ext_attr_entry);
-               
+
                /* is attribute name valid? */
                if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
                        pctx->num = entry->e_name_len;
@@ -291,9 +317,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 +337,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 +362,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 
+#endif
        /* i_extra_isize must cover i_extra_isize + i_pad1 at least */
        min = sizeof(inode->i_extra_isize) + sizeof(inode->i_pad1);
        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 +388,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
@@ -378,7 +404,7 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
        const char              *old_op;
        errcode_t               retval;
        blk_t                   blk;
-       int                     i, not_device = 0;
+       unsigned int            i, rec_len, not_device = 0;
 
        if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
            LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
@@ -397,7 +423,7 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
                        return; /* Invalid block, can't be dir */
        }
 
-       if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) && 
+       if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
            (inode->i_links_count == 1) && !not_device)
                return;
 
@@ -408,31 +434,37 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
                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;
@@ -480,13 +512,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;
-       int             imagic_fs;
+       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))
@@ -514,6 +544,7 @@ void e2fsck_pass1(e2fsck_t ctx)
 #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
@@ -597,7 +628,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                                                    "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) {
@@ -621,7 +652,7 @@ void e2fsck_pass1(e2fsck_t ctx)
 
        while (1) {
                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)
@@ -644,7 +675,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 +684,79 @@ 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_bitmap(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;
-                       
+
                        pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
                                                          &pb.fs_meta_blocks);
                        if (pctx.errcode) {
@@ -672,7 +773,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_iterate2(fs, ino, 0,
                                     block_buf, process_bad_block, &pb);
                        ext2fs_free_block_bitmap(pb.fs_meta_blocks);
                        if (pctx.errcode) {
@@ -695,10 +796,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
@@ -731,17 +830,17 @@ void e2fsck_pass1(e2fsck_t ctx)
                        }
                        if ((inode->i_links_count || inode->i_blocks ||
                             inode->i_blocks || inode->i_block[0]) &&
-                           fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR, 
+                           fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
                                        &pctx)) {
                                memset(inode, 0, inode_size);
                                ext2fs_icount_store(ctx->inode_link_info,
                                                    ino, 0);
-                               e2fsck_write_inode_full(ctx, ino, inode, 
+                               e2fsck_write_inode_full(ctx, ino, inode,
                                                        inode_size, "pass1");
                        }
                } else if (ino < EXT2_FIRST_INODE(fs->super)) {
                        int     problem = 0;
-                       
+
                        ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
                        if (ino == EXT2_BOOT_LOADER_INO) {
                                if (LINUX_S_ISDIR(inode->i_mode))
@@ -769,7 +868,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                 * 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 +889,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 +913,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,7 +921,7 @@ void e2fsck_pass1(e2fsck_t ctx)
                                e2fsck_write_inode(ctx, ino, inode, "pass1");
                        }
                }
-               
+
                ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
                switch (fs->super->s_creator_os) {
                    case EXT2_OS_HURD:
@@ -832,12 +931,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);
@@ -859,6 +962,18 @@ void e2fsck_pass1(e2fsck_t ctx)
                check_inode_extra_space(ctx, &pctx);
                check_is_really_dir(ctx, &pctx, block_buf);
 
+               /*
+                * ext2fs_inode_has_valid_blocks 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) &&
+                   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);
                        e2fsck_add_dir_info(ctx, ino, 0);
@@ -877,7 +992,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 +1014,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] ||
+                    inode->i_file_acl)) {
                        inodes_to_process[process_inode_count].ino = ino;
                        inodes_to_process[process_inode_count].inode = *inode;
                        process_inode_count++;
@@ -943,7 +1062,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);
 
@@ -969,12 +1088,12 @@ void e2fsck_pass1(e2fsck_t ctx)
                e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
                                  "recreate inode");
                inode->i_mtime = ctx->now;
-               e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode, 
+               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 +1116,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 +1136,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 +1158,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 +1173,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 +1200,13 @@ 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;
+       if (ret == 0)
+               ret = ib_a->ino - ib_b->ino;
        return ret;
 }
 
@@ -1103,7 +1219,7 @@ 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);
                if (pctx.errcode) {
@@ -1124,7 +1240,7 @@ 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"),
@@ -1144,7 +1260,7 @@ 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"),
@@ -1168,9 +1284,9 @@ static void alloc_imagic_map(e2fsck_t ctx)
 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
 {
        struct          problem_context pctx;
-       
+
        clear_problem_context(&pctx);
-       
+
        if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
                if (!ctx->block_dup_map) {
                        pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
@@ -1178,7 +1294,7 @@ static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
                              &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;
@@ -1198,7 +1314,7 @@ 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;
@@ -1209,7 +1325,7 @@ static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
        int                             count;
 
        clear_problem_context(&pctx);
-       
+
        ea_refcount_intr_begin(refcount);
        while (1) {
                if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
@@ -1355,10 +1471,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 +1485,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 +1502,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)) {
@@ -1407,8 +1538,7 @@ clear_extattr:
 
 /* 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;
@@ -1422,17 +1552,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_bmap(fs, ino, inode, 0, 0, 0, &blk);
+
+       if ((pctx->errcode) ||
+           (blk == 0) ||
+           (blk < fs->super->s_first_data_block) ||
+           (blk >= fs->super->s_blocks_count)) {
+               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);
        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 +1581,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 +1590,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,7 +1598,7 @@ 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;
@@ -1484,6 +1619,179 @@ void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
        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;
+       blk_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 < ctx->fs->super->s_first_data_block ||
+                   extent.e_pblk >= ctx->fs->super->s_blocks_count)
+                       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_pblk + extent.e_len) >
+                        ctx->fs->super->s_blocks_count)
+                       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)) {
+                               pctx->errcode =
+                                       ext2fs_extent_delete(ehandle, 0);
+                               if (pctx->errcode) {
+                                       pctx->str = "ext2fs_extent_delete";
+                                       return;
+                               }
+                               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) {
+                       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)
+                                       goto report_problem;
+                               return;
+                       }
+                       scan_extent_node(ctx, pctx, pb, extent.e_lblk, ehandle);
+                       if (pctx->errcode)
+                               return;
+                       pctx->errcode = ext2fs_extent_get(ehandle,
+                                                 EXT2_EXTENT_UP, &extent);
+                       if (pctx->errcode) {
+                               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;
+               }
+               for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
+                    i < extent.e_len;
+                    blk++, blockcnt++, i++) {
+                       mark_block_used(ctx, blk);
+
+                       if (is_dir) {
+                               pctx->errcode = ext2fs_add_dir_block(ctx->fs->dblist, pctx->ino, blk, blockcnt);
+                               if (pctx->errcode) {
+                                       pctx->blk = blk;
+                                       pctx->num = blockcnt;
+                                       fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
+                                       /* Should never get here */
+                                       ctx->flags |= E2F_FLAG_ABORT;
+                                       return;
+                               }
+                       }
+               }
+               pb->num_blocks += extent.e_len;
+               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;
+       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,8 +1805,9 @@ 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;
@@ -1516,6 +1825,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 +1840,20 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                }
        }
 
-       if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf))
+       if (inode->i_file_acl && 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_blocks(inode)) {
+               if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
+                       check_blocks_extents(ctx, pctx, &pb);
+               else
+                       pctx->errcode = ext2fs_block_iterate2(fs, ino,
+                                               pb.is_dir ? BLOCK_FLAG_HOLE : 0,
+                                               block_buf, process_block, &pb);
+       }
        end_problem_latch(ctx, PR_LATCH_BLOCK);
        end_problem_latch(ctx, PR_LATCH_TOOBIG);
        if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
@@ -1542,15 +1861,39 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        if (pctx->errcode)
                fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
 
-       if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
-               ctx->fs_fragmented++;
+       if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
+               if (LINUX_S_ISDIR(inode->i_mode))
+                       ctx->fs_fragmented_dir++;
+               else
+                       ctx->fs_fragmented++;
+       }
 
        if (pb.clear) {
                e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
                                   "check_blocks");
                return;
        }
-       
+
+       if (pb.is_dir) {
+               while (1) {
+                       struct ext2_db_entry *entry;
+
+                       if (ext2fs_dblist_get_last(fs->dblist, &entry) ||
+                           (entry->ino != ino) ||
+                           (entry->blk != 0) ||
+                           (entry->blockcnt == 0))
+                               break;
+                       /* printf("Dropping ino %lu blk %lu blockcnt %d\n",
+                                 entry->ino, entry->blk, entry->blockcnt); */
+                       ext2fs_dblist_drop_last(fs->dblist);
+                       if (ext2fs_dblist_get_last(fs->dblist, &entry) ||
+                           (entry->ino != ino))
+                               pb.last_block--;
+                       else
+                               pb.last_block = entry->blockcnt;
+               }
+       }
+
        if (inode->i_flags & EXT2_INDEX_FL) {
                if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
                        inode->i_flags &= ~EXT2_INDEX_FL;
@@ -1565,7 +1908,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
            !(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,7 +1917,10 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                }
        }
 
-       pb.num_blocks *= (fs->blocksize / 512);
+       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);
 #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,
@@ -1582,7 +1928,9 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
 #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) >
@@ -1599,12 +1947,20 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                    (pb.last_block / blkpg * blkpg != pb.last_block ||
                     size < (__u64)(pb.last_block & ~(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))
@@ -1616,10 +1972,15 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        if (LINUX_S_ISREG(inode->i_mode) &&
            (inode->i_size_high || inode->i_size & 0x80000000UL))
                ctx->large_files++;
-       if (pb.num_blocks != inode->i_blocks) {
+       if ((pb.num_blocks != inode->i_blocks) ||
+           ((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 = 0;
                        dirty_inode++;
                }
                pctx->num = 0;
@@ -1744,6 +2105,7 @@ static int process_block(ext2_filsys fs,
                        printf("Missing block (#%d) in directory inode %lu!\n",
                               blockcnt, p->ino);
 #endif
+                       p->last_block = blockcnt;
                        goto mark_dir;
                }
                return 0;
@@ -1753,15 +2115,31 @@ static int process_block(ext2_filsys fs,
        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;
 
@@ -1771,7 +2149,7 @@ 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)
                problem = PR_1_ILLEGAL_BLOCK_NUM;
@@ -1800,7 +2178,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
@@ -1851,11 +2229,11 @@ 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;
@@ -1876,10 +2254,10 @@ static int process_bad_block(ext2_filsys fs,
                                *block_nr = 0;
                                return BLOCK_CHANGED;
                        }
-               } else if (ext2fs_test_block_bitmap(ctx->block_found_map, 
+               } else if (ext2fs_test_block_bitmap(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 +2268,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++;
@@ -1908,7 +2286,7 @@ static int process_bad_block(ext2_filsys fs,
         * 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;
@@ -1994,7 +2372,7 @@ static int process_bad_block(ext2_filsys fs,
        return 0;
 }
 
-static void new_table_block(e2fsck_t ctx, blk_t first_block, int group, 
+static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
                            const char *name, int num, blk_t *new_block)
 {
        ext2_filsys fs = ctx->fs;
@@ -2077,7 +2455,7 @@ static void handle_fs_bad_blocks(e2fsck_t ctx)
                }
                if (ctx->invalid_inode_table_flag[i]) {
                        new_table_block(ctx, first_block, i, _("inode table"),
-                                       fs->inode_blocks_per_group, 
+                                       fs->inode_blocks_per_group,
                                        &fs->group_desc[i].bg_inode_table);
                        ctx->flags |= E2F_FLAG_RESTART;
                }
@@ -2096,9 +2474,9 @@ static void mark_table_blocks(e2fsck_t ctx)
        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;
 
@@ -2125,9 +2503,9 @@ static void mark_table_blocks(e2fsck_t ctx)
                                }
                        }
                }
-                           
+
                /*
-                * 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,
@@ -2141,10 +2519,10 @@ static void mark_table_blocks(e2fsck_t ctx)
                            ext2fs_mark_block_bitmap(ctx->block_found_map,
                                     fs->group_desc[i].bg_block_bitmap);
                    }
-                       
+
                }
                /*
-                * 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,
@@ -2153,7 +2531,7 @@ static void mark_table_blocks(e2fsck_t ctx)
                                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);
@@ -2161,7 +2539,7 @@ static void mark_table_blocks(e2fsck_t ctx)
                }
        }
 }
-       
+
 /*
  * Thes subroutines short circuits ext2fs_get_blocks and
  * ext2fs_check_directory; we use them since we already have the inode
@@ -2173,7 +2551,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,6 +2594,48 @@ static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
        return 0;
 }
 
+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;
+       blk_t           new_block;
+
+       if (ctx->block_found_map) {
+               retval = ext2fs_new_block(fs, (blk_t) goal,
+                                         ctx->block_found_map, &new_block);
+               if (retval)
+                       return retval;
+       } else {
+               if (!fs->block_map) {
+                       retval = ext2fs_read_block_bitmap(fs);
+                       if (retval)
+                               return retval;
+               }
+
+               retval = ext2fs_new_block(fs, (blk_t) 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_bitmap(ctx->block_found_map,
+                                                (blk_t) blk);
+               else
+                       ext2fs_unmark_block_bitmap(ctx->block_found_map,
+                                                  (blk_t) blk);
+       }
+}
+
 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
 {
        ext2_filsys fs = ctx->fs;
@@ -2226,6 +2646,11 @@ void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
                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;