Whamcloud - gitweb
blkid: Add new option -L which pretty-prints the device list
[tools/e2fsprogs.git] / e2fsck / pass1.c
index c8eee89..c240195 100644 (file)
@@ -37,6 +37,8 @@
  * found.
  */
 
+#define _GNU_SOURCE 1 /* get strnlen() */
+#include <string.h>
 #include <time.h>
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
@@ -76,8 +78,8 @@ static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
 
 struct process_block_struct {
        ext2_ino_t      ino;
-       int             is_dir:1, is_reg:1, clear:1, suppress:1,
-                               fragmented:1, compressed:1;
+       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;
        e2_blkcnt_t     last_block;
@@ -85,6 +87,7 @@ struct process_block_struct {
        blk_t           previous_block;
        struct ext2_inode *inode;
        struct problem_context *pctx;
+       ext2fs_block_bitmap fs_meta_blocks;
        e2fsck_t        ctx;
 };
 
@@ -111,9 +114,9 @@ static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
  * Free all memory allocated by pass1 in preparation for restarting
  * things.
  */
-static void unwind_pass1(ext2_filsys fs)
+static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
 {
-       ext2fs_free_mem((void **) &inodes_to_process);
+       ext2fs_free_mem(&inodes_to_process);
        inodes_to_process = 0;
 }
 
@@ -125,15 +128,16 @@ static void unwind_pass1(ext2_filsys fs)
  * since they have the same requirement; the i_block fields should be
  * zero. 
  */
-int e2fsck_pass1_check_device_inode(struct ext2_inode *inode)
+int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)), 
+                                   struct ext2_inode *inode)
 {
        int     i;
 
        /*
-        * If i_blocks is non-zero, or the index flag is set, then
-        * this is a bogus device/fifo/socket
+        * If the index flag is set, then this is a bogus
+        * device/fifo/socket
         */
-       if (inode->i_blocks || (inode->i_flags & EXT2_INDEX_FL))
+       if (inode->i_flags & EXT2_INDEX_FL)
                return 0;
 
        /*
@@ -155,38 +159,50 @@ int e2fsck_pass1_check_device_inode(struct ext2_inode *inode)
        return 1;
 }
 
-#ifndef HAVE_STRNLEN
-/*
- * Incredibly, libc5 doesn't appear to have strnlen.  So we have to
- * provide our own.
- */
-static int strnlen(const char * s, int count)
-{
-       const char *cp = s;
-
-       while (count-- && *cp)
-               cp++;
-       return cp - s;
-}
-#endif
-
 /*
  * 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)
 {
-       int len;
+       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_blocks) {
+       if (inode->i_flags & EXT4_EXTENTS_FL) {
+               if (inode->i_size > fs->blocksize)
+                       return 0;
+               if (ext2fs_extent_open(fs, ino, &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) ||
-                   (inode->i_blocks != fs->blocksize >> 9) ||
+                   (blocks != fs->blocksize >> 9) ||
                    (inode->i_block[0] < fs->super->s_first_data_block) ||
                    (inode->i_block[0] >= fs->super->s_blocks_count))
                        return 0;
@@ -250,6 +266,228 @@ static void check_size(e2fsck_t ctx, struct problem_context *pctx)
        e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
 }
        
+static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
+{
+       struct ext2_super_block *sb = ctx->fs->super;
+       struct ext2_inode_large *inode;
+       struct ext2_ext_attr_entry *entry;
+       char *start, *end;
+       unsigned int storage_size, remain;
+       int problem = 0;
+
+       inode = (struct ext2_inode_large *) pctx->inode;
+       storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
+               inode->i_extra_isize;
+       start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
+               inode->i_extra_isize + sizeof(__u32);
+       end = (char *) inode + EXT2_INODE_SIZE(ctx->fs->super);
+       entry = (struct ext2_ext_attr_entry *) start;
+
+       /* scan all entry's headers first */
+
+       /* take finish entry 0UL into account */
+       remain = storage_size - sizeof(__u32); 
+
+       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;
+                       problem = PR_1_ATTR_NAME_LEN;
+                       goto fix;
+               }
+
+               /* attribute len eats this space */
+               remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
+
+               /* check value size */
+               if (entry->e_value_size == 0 || entry->e_value_size > remain) {
+                       pctx->num = entry->e_value_size;
+                       problem = PR_1_ATTR_VALUE_SIZE;
+                       goto fix;
+               }
+
+               /* e_value_block must be 0 in inode's ea */
+               if (entry->e_value_block != 0) {
+                       pctx->num = entry->e_value_block;
+                       problem = PR_1_ATTR_VALUE_BLOCK;
+                       goto fix;
+               }
+
+               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;
+               }
+
+               remain -= entry->e_value_size;
+
+               entry = EXT2_EXT_ATTR_NEXT(entry);
+       }
+fix:
+       /*
+        * it seems like a corruption. it's very unlikely we could repair
+        * EA(s) in automatic fashion -bzzz
+        */
+       if (problem == 0 || !fix_problem(ctx, problem, pctx))
+               return;
+
+       /* simply remove all possible EA(s) */
+       *((__u32 *)start) = 0UL;
+       e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
+                               EXT2_INODE_SIZE(sb), "pass1");
+}
+
+static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
+{
+       struct ext2_super_block *sb = ctx->fs->super;
+       struct ext2_inode_large *inode;
+       __u32 *eamagic;
+       int min, max;
+
+       inode = (struct ext2_inode_large *) pctx->inode;
+       if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
+               /* this isn't large inode. so, nothing to check */
+               return;
+       }
+
+#if 0
+       printf("inode #%u, i_extra_size %d\n", pctx->ino,
+                       inode->i_extra_isize);
+#endif 
+       /* i_extra_isize must cover i_extra_isize + i_pad1 at least */
+       min = sizeof(inode->i_extra_isize) + sizeof(inode->i_pad1);
+       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
+        */
+       if (inode->i_extra_isize &&
+           (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
+               if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
+                       return;
+               inode->i_extra_isize = min;
+               e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
+                                       EXT2_INODE_SIZE(sb), "pass1");
+               return;
+       }
+
+       eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
+                       inode->i_extra_isize);
+       if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
+               /* it seems inode has an extended attribute(s) in body */
+               check_ea_in_inode(ctx, 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
+ * convinced happens frequently in the wild.  If for any reason this
+ * causes any problems, take this code out.
+ * [tytso:20070331.0827EDT]
+ */
+static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
+                               char *buf)
+{
+       struct ext2_inode *inode = pctx->inode;
+       struct ext2_dir_entry   *dirent;
+       const char              *old_op;
+       errcode_t               retval;
+       blk_t                   blk;
+       int                     i, not_device = 0;
+
+       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++;
+
+               if (blk < ctx->fs->super->s_first_data_block ||
+                   blk >= ctx->fs->super->s_blocks_count ||
+                   ext2fs_fast_test_block_bitmap(ctx->block_found_map, blk))
+                       return; /* Invalid block, can't be dir */
+       }
+
+       if ((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);
+       ehandler_operation(0);
+       if (retval)
+               return;
+
+       dirent = (struct ext2_dir_entry *) buf;
+       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))
+               return;
+
+       dirent = (struct ext2_dir_entry *) (buf + dirent->rec_len);
+       if (((dirent->name_len & 0xFF) != 2) ||
+           (dirent->name[0] != '.') ||
+           (dirent->name[1] != '.') ||
+           (dirent->rec_len < 12) ||
+           (dirent->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), 
+                                       "check_is_really_dir");
+       }
+}
+
+extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags, 
+                                   ext2_icount_t *ret)
+{
+       unsigned int            threshold;
+       ext2_ino_t              num_dirs;
+       errcode_t               retval;
+       char                    *tdb_dir;
+       int                     enable;
+
+       *ret = 0;
+
+       profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
+                          &tdb_dir);
+       profile_get_uint(ctx->profile, "scratch_files",
+                        "numdirs_threshold", 0, 0, &threshold);
+       profile_get_boolean(ctx->profile, "scratch_files",
+                           "icount", 0, 1, &enable);
+
+       retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
+       if (retval)
+               num_dirs = 1024;        /* Guess */
+
+       if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
+           (threshold && num_dirs <= threshold))
+               return;
+
+       retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
+       if (retval)
+               *ret = 0;
+}
 
 void e2fsck_pass1(e2fsck_t ctx)
 {
@@ -257,7 +495,7 @@ void e2fsck_pass1(e2fsck_t ctx)
        __u64   max_sizes;
        ext2_filsys fs = ctx->fs;
        ext2_ino_t      ino;
-       struct ext2_inode inode;
+       struct ext2_inode *inode;
        ext2_inode_scan scan;
        char            *block_buf;
 #ifdef RESOURCE_TRACK
@@ -267,16 +505,25 @@ void e2fsck_pass1(e2fsck_t ctx)
        struct          problem_context pctx;
        struct          scan_callback_struct scan_struct;
        struct ext2_super_block *sb = ctx->fs->super;
-       int             imagic_fs;
+       const char      *old_op;
+       int             imagic_fs, extent_fs;
+       int             busted_fs_time = 0;
+       int             inode_size;
        
 #ifdef RESOURCE_TRACK
-       init_resource_track(&rtrack);
+       init_resource_track(&rtrack, ctx->fs->io);
 #endif
        clear_problem_context(&pctx);
 
        if (!(ctx->options & E2F_OPT_PREEN))
                fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
 
+       if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
+           !(ctx->options & E2F_OPT_NO)) {
+               if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
+                       ctx->dirs_to_hash = 0;
+       }
+
 #ifdef MTRACE
        mtrace_print("Pass 1");
 #endif
@@ -293,6 +540,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
@@ -329,13 +577,19 @@ void e2fsck_pass1(e2fsck_t ctx)
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
-       pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
-                                            &ctx->inode_link_info);
+       e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
+       if (!ctx->inode_link_info)
+               pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
+                                                    &ctx->inode_link_info);
        if (pctx.errcode) {
                fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
+       inode_size = EXT2_INODE_SIZE(fs->super);
+       inode = (struct ext2_inode *)
+               e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
+
        inodes_to_process = (struct process_inode_block *)
                e2fsck_allocate_memory(ctx,
                                       (ctx->process_inode_size *
@@ -347,6 +601,7 @@ void e2fsck_pass1(e2fsck_t ctx)
        if (pctx.errcode) {
                fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
+               ext2fs_free_mem(&inode);
                return;
        }
 
@@ -368,79 +623,177 @@ void e2fsck_pass1(e2fsck_t ctx)
        block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
                                                    "block interate buffer");
        e2fsck_use_inode_shortcuts(ctx, 1);
-       ehandler_operation(_("doing inode scan"));
+       old_op = ehandler_operation(_("opening inode scan"));
        pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks, 
                                              &scan);
+       ehandler_operation(old_op);
        if (pctx.errcode) {
                fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
+               ext2fs_free_mem(&block_buf);
+               ext2fs_free_mem(&inode);
                return;
        }
        ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
-       pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
-       if (pctx.errcode) {
-               fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
-               ctx->flags |= E2F_FLAG_ABORT;
-               return;
-       }
-       ctx->stashed_inode = &inode;
+       ctx->stashed_inode = inode;
        scan_struct.ctx = ctx;
        scan_struct.block_buf = block_buf;
        ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
        if (ctx->progress)
                if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
                        return;
-       while (ino) {
+       if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
+           (fs->super->s_mtime < fs->super->s_inodes_count))
+               busted_fs_time = 1;
+
+       while (1) {
+               old_op = ehandler_operation(_("getting next inode from scan"));
+               pctx.errcode = ext2fs_get_next_inode_full(scan, &ino, 
+                                                         inode, inode_size);
+               ehandler_operation(old_op);
+               if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
+                       return;
+               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);
+                       continue;
+               }
+               if (pctx.errcode) {
+                       fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
+                       ctx->flags |= E2F_FLAG_ABORT;
+                       return;
+               }
+               if (!ino)
+                       break;
                pctx.ino = ino;
-               pctx.inode = &inode;
+               pctx.inode = inode;
                ctx->stashed_ino = ino;
-               if (inode.i_links_count) {
+               if (inode->i_links_count) {
                        pctx.errcode = ext2fs_icount_store(ctx->inode_link_info, 
-                                          ino, inode.i_links_count);
+                                          ino, inode->i_links_count);
                        if (pctx.errcode) {
-                               pctx.num = inode.i_links_count;
+                               pctx.num = inode->i_links_count;
                                fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
                                ctx->flags |= E2F_FLAG_ABORT;
                                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) {
+                               pctx.num = 4;
+                               fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
+                               ctx->flags |= E2F_FLAG_ABORT;
+                               return;
+                       }
                        pb.ino = EXT2_BAD_INO;
                        pb.num_blocks = pb.last_block = 0;
                        pb.num_illegal_blocks = 0;
                        pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
-                       pb.is_reg = 0; pb.fragmented = 0;
-                       pb.inode = &inode;
+                       pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
+                       pb.inode = inode;
                        pb.pctx = &pctx;
                        pb.ctx = ctx;
                        pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, 
                                     block_buf, process_bad_block, &pb);
+                       ext2fs_free_block_bitmap(pb.fs_meta_blocks);
                        if (pctx.errcode) {
                                fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
                                ctx->flags |= E2F_FLAG_ABORT;
                                return;
                        }
+                       if (pb.bbcheck)
+                               if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
+                               ctx->flags |= E2F_FLAG_ABORT;
+                               return;
+                       }
                        ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
                        clear_problem_context(&pctx);
-                       goto next;
+                       continue;
                } else if (ino == EXT2_ROOT_INO) {
                        /*
                         * Make sure the root inode is a directory; if
                         * not, offer to clear it.  It will be
                         * regnerated in pass #3.
                         */
-                       if (!LINUX_S_ISDIR(inode.i_mode)) {
-                               if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
-                                       inode.i_dtime = time(0);
-                                       inode.i_links_count = 0;
-                                       ext2fs_icount_store(ctx->inode_link_info,
-                                                           ino, 0);
-                                       e2fsck_write_inode(ctx, ino, &inode,
-                                                          "pass1");
-                               }
-
+                       if (!LINUX_S_ISDIR(inode->i_mode)) {
+                               if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
+                                       goto clear_inode;
                        }
                        /*
                         * If dtime is set, offer to clear it.  mke2fs
@@ -451,59 +804,60 @@ void e2fsck_pass1(e2fsck_t ctx)
                         * easily.  But we will fix the root directory
                         * as a special case.
                         */
-                       if (inode.i_dtime && inode.i_links_count) {
+                       if (inode->i_dtime && inode->i_links_count) {
                                if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
-                                       inode.i_dtime = 0;
-                                       e2fsck_write_inode(ctx, ino, &inode,
+                                       inode->i_dtime = 0;
+                                       e2fsck_write_inode(ctx, ino, inode,
                                                           "pass1");
                                }
                        }
                } else if (ino == EXT2_JOURNAL_INO) {
                        ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
                        if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
-                               if (!LINUX_S_ISREG(inode.i_mode) &&
+                               if (!LINUX_S_ISREG(inode->i_mode) &&
                                    fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
                                                &pctx)) {
-                                       inode.i_mode = LINUX_S_IFREG;
-                                       e2fsck_write_inode(ctx, ino, &inode,
+                                       inode->i_mode = LINUX_S_IFREG;
+                                       e2fsck_write_inode(ctx, ino, inode,
                                                           "pass1");
                                }
                                check_blocks(ctx, &pctx, block_buf);
-                               goto next;
+                               continue;
                        }
-                       if ((inode.i_links_count || inode.i_blocks ||
-                            inode.i_blocks || inode.i_block[0]) &&
+                       if ((inode->i_links_count || inode->i_blocks ||
+                            inode->i_blocks || inode->i_block[0]) &&
                            fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR, 
                                        &pctx)) {
-                               memset(&inode, 0, sizeof(inode));
+                               memset(inode, 0, inode_size);
                                ext2fs_icount_store(ctx->inode_link_info,
                                                    ino, 0);
-                               e2fsck_write_inode(ctx, ino, &inode, "pass1");
+                               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))
+                               if (LINUX_S_ISDIR(inode->i_mode))
                                        problem = PR_1_RESERVED_BAD_MODE;
                        } else if (ino == EXT2_RESIZE_INO) {
-                               if (inode.i_mode &&
-                                   !LINUX_S_ISREG(inode.i_mode))
+                               if (inode->i_mode &&
+                                   !LINUX_S_ISREG(inode->i_mode))
                                        problem = PR_1_RESERVED_BAD_MODE;
                        } else {
-                               if (inode.i_mode != 0)
+                               if (inode->i_mode != 0)
                                        problem = PR_1_RESERVED_BAD_MODE;
                        }
                        if (problem) {
                                if (fix_problem(ctx, problem, &pctx)) {
-                                       inode.i_mode = 0;
-                                       e2fsck_write_inode(ctx, ino, &inode,
+                                       inode->i_mode = 0;
+                                       e2fsck_write_inode(ctx, ino, inode,
                                                           "pass1");
                                }
                        }
                        check_blocks(ctx, &pctx, block_buf);
-                       goto next;
+                       continue;
                }
                /*
                 * Check for inodes who might have been part of the
@@ -522,12 +876,12 @@ void e2fsck_pass1(e2fsck_t ctx)
                 * than nothing.  The right answer is that there
                 * shouldn't be any bugs in the orphan list handling.  :-)
                 */
-               if (inode.i_dtime &&
-                   inode.i_dtime < ctx->fs->super->s_inodes_count) {
+               if (inode->i_dtime && !busted_fs_time &&
+                   inode->i_dtime < ctx->fs->super->s_inodes_count) {
                        if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
-                               inode.i_dtime = inode.i_links_count ?
-                                       0 : time(0);
-                               e2fsck_write_inode(ctx, ino, &inode,
+                               inode->i_dtime = inode->i_links_count ?
+                                       0 : ctx->now;
+                               e2fsck_write_inode(ctx, ino, inode,
                                                   "pass1");
                        }
                }
@@ -536,16 +890,16 @@ void e2fsck_pass1(e2fsck_t ctx)
                 * This code assumes that deleted inodes have
                 * i_links_count set to 0.  
                 */
-               if (!inode.i_links_count) {
-                       if (!inode.i_dtime && inode.i_mode) {
+               if (!inode->i_links_count) {
+                       if (!inode->i_dtime && inode->i_mode) {
                                if (fix_problem(ctx,
                                            PR_1_ZERO_DTIME, &pctx)) {
-                                       inode.i_dtime = time(0);
-                                       e2fsck_write_inode(ctx, ino, &inode,
+                                       inode->i_dtime = ctx->now;
+                                       e2fsck_write_inode(ctx, ino, inode,
                                                           "pass1");
                                }
                        }
-                       goto next;
+                       continue;
                }
                /*
                 * n.b.  0.3c ext2fs code didn't clear i_links_count for
@@ -557,35 +911,32 @@ void e2fsck_pass1(e2fsck_t ctx)
                 * should keep the file, not delete it.
                 * 
                 */
-               if (inode.i_dtime) {
+               if (inode->i_dtime) {
                        if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
-                               inode.i_dtime = 0;
-                               e2fsck_write_inode(ctx, ino, &inode, "pass1");
+                               inode->i_dtime = 0;
+                               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_LINUX:
-                       frag = inode.osd2.linux2.l_i_frag;
-                       fsize = inode.osd2.linux2.l_i_fsize;
-                       break;
                    case EXT2_OS_HURD:
-                       frag = inode.osd2.hurd2.h_i_frag;
-                       fsize = inode.osd2.hurd2.h_i_fsize;
-                       break;
-                   case EXT2_OS_MASIX:
-                       frag = inode.osd2.masix2.m_i_frag;
-                       fsize = inode.osd2.masix2.m_i_fsize;
+                       frag = inode->osd2.hurd2.h_i_frag;
+                       fsize = inode->osd2.hurd2.h_i_fsize;
                        break;
                    default:
                        frag = fsize = 0;
                }
                
-               if (inode.i_faddr || frag || fsize ||
-                   (LINUX_S_ISDIR(inode.i_mode) && inode.i_dir_acl))
+               if (inode->i_faddr || frag || fsize ||
+                   (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
                        mark_inode_bad(ctx, ino);
-               if (inode.i_flags & EXT2_IMAGIC_FL) {
+               if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
+                   !(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);
+               if (inode->i_flags & EXT2_IMAGIC_FL) {
                        if (imagic_fs) {
                                if (!ctx->inode_imagic_map)
                                        alloc_imagic_map(ctx);
@@ -593,63 +944,81 @@ void e2fsck_pass1(e2fsck_t ctx)
                                                         ino);
                        } else {
                                if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
-                                       inode.i_flags &= ~EXT2_IMAGIC_FL;
+                                       inode->i_flags &= ~EXT2_IMAGIC_FL;
                                        e2fsck_write_inode(ctx, ino,
-                                                          &inode, "pass1");
+                                                          inode, "pass1");
                                }
                        }
                }
-               
-               if (LINUX_S_ISDIR(inode.i_mode)) {
+
+               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);
                        ctx->fs_directory_count++;
-               } else if (LINUX_S_ISREG (inode.i_mode)) {
+               } else if (LINUX_S_ISREG (inode->i_mode)) {
                        ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
                        ctx->fs_regular_count++;
-               } else if (LINUX_S_ISCHR (inode.i_mode) &&
-                          e2fsck_pass1_check_device_inode(&inode)) {
+               } else if (LINUX_S_ISCHR (inode->i_mode) &&
+                          e2fsck_pass1_check_device_inode(fs, inode)) {
                        check_immutable(ctx, &pctx);
                        check_size(ctx, &pctx);
                        ctx->fs_chardev_count++;
-               } else if (LINUX_S_ISBLK (inode.i_mode) &&
-                          e2fsck_pass1_check_device_inode(&inode)) {
+               } else if (LINUX_S_ISBLK (inode->i_mode) &&
+                          e2fsck_pass1_check_device_inode(fs, inode)) {
                        check_immutable(ctx, &pctx);
                        check_size(ctx, &pctx);
                        ctx->fs_blockdev_count++;
-               } else if (LINUX_S_ISLNK (inode.i_mode) &&
-                          e2fsck_pass1_check_symlink(fs, &inode, block_buf)) {
+               } else if (LINUX_S_ISLNK (inode->i_mode) &&
+                          e2fsck_pass1_check_symlink(fs, ino, inode, 
+                                                     block_buf)) {
                        check_immutable(ctx, &pctx);
                        ctx->fs_symlinks_count++;
-                       if (!inode.i_blocks) {
+                       if (ext2fs_inode_data_blocks(fs, inode) == 0) {
                                ctx->fs_fast_symlinks_count++;
-                               goto next;
+                               check_blocks(ctx, &pctx, block_buf);
+                               continue;
                        }
                }
-               else if (LINUX_S_ISFIFO (inode.i_mode) &&
-                        e2fsck_pass1_check_device_inode(&inode)) {
+               else if (LINUX_S_ISFIFO (inode->i_mode) &&
+                        e2fsck_pass1_check_device_inode(fs, inode)) {
                        check_immutable(ctx, &pctx);
                        check_size(ctx, &pctx);
                        ctx->fs_fifo_count++;
-               } else if ((LINUX_S_ISSOCK (inode.i_mode)) &&
-                          e2fsck_pass1_check_device_inode(&inode)) {
+               } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
+                          e2fsck_pass1_check_device_inode(fs, inode)) {
                        check_immutable(ctx, &pctx);
                        check_size(ctx, &pctx);
                        ctx->fs_sockets_count++;
                } else
                        mark_inode_bad(ctx, ino);
-               if (inode.i_block[EXT2_IND_BLOCK])
+               if (inode->i_block[EXT2_IND_BLOCK])
                        ctx->fs_ind_count++;
-               if (inode.i_block[EXT2_DIND_BLOCK])
+               if (inode->i_block[EXT2_DIND_BLOCK])
                        ctx->fs_dind_count++;
-               if (inode.i_block[EXT2_TIND_BLOCK])
+               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) &&
+                   (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;
+                       inodes_to_process[process_inode_count].inode = *inode;
                        process_inode_count++;
                } else
                        check_blocks(ctx, &pctx, block_buf);
@@ -663,26 +1032,9 @@ void e2fsck_pass1(e2fsck_t ctx)
                        if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
                                return;
                }
-       next:
-               pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
-               if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
-                       return;
-               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);
-                       goto next;
-               }
-               if (pctx.errcode) {
-                       fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
-                       ctx->flags |= E2F_FLAG_ABORT;
-                       return;
-               }
        }
        process_inodes(ctx, block_buf);
        ext2fs_close_inode_scan(scan);
-       ehandler_operation(0);
 
        /*
         * If any extended attribute blocks' reference counts need to
@@ -710,6 +1062,28 @@ void e2fsck_pass1(e2fsck_t ctx)
                ctx->block_ea_map = 0;
        }
 
+       if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
+               ext2fs_block_bitmap save_bmap;
+
+               save_bmap = fs->block_map;
+               fs->block_map = ctx->block_found_map;
+               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;
+               }
+               e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
+                                 "recreate inode");
+               inode->i_mtime = ctx->now;
+               e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode, 
+                                  "recreate inode");
+               fs->block_map = save_bmap;
+               ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
+       }
+                      
        if (ctx->flags & E2F_FLAG_RESTART) {
                /*
                 * Only the master copy of the superblock and block
@@ -729,16 +1103,17 @@ void e2fsck_pass1(e2fsck_t ctx)
                }
                e2fsck_pass1_dupblocks(ctx, block_buf);
        }
-       ext2fs_free_mem((void **) &inodes_to_process);
+       ext2fs_free_mem(&inodes_to_process);
 endit:
        e2fsck_use_inode_shortcuts(ctx, 0);
        
-       ext2fs_free_mem((void **) &block_buf);
+       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);
+               print_resource_track(_("Pass 1"), &rtrack, ctx->fs->io);
        }
 #endif
 }
@@ -747,7 +1122,8 @@ endit:
  * 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, ext2_inode_scan scan,
+static errcode_t scan_callback(ext2_filsys fs, 
+                              ext2_inode_scan scan EXT2FS_ATTR((unused)),
                               dgrp_t group, void * priv_data)
 {
        struct scan_callback_struct *scan_struct;
@@ -983,8 +1359,8 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
        struct ext2_ext_attr_header *header;
        struct ext2_ext_attr_entry *entry;
        int             count;
-       region_t        region;
-       
+       region_t        region = 0;
+
        blk = inode->i_file_acl;
        if (blk == 0)
                return 0;
@@ -1050,7 +1426,7 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
                ea_refcount_increment(ctx->refcount_extra, blk, 0);
                return 1;
        }
-       
+
        /*
         * OK, we haven't seen this EA block yet.  So we need to
         * validate it
@@ -1061,10 +1437,14 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
                goto clear_extattr;
        header = (struct ext2_ext_attr_header *) block_buf;
        pctx->blk = inode->i_file_acl;
-       if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
+       if (((ctx->ext_attr_ver == 1) &&
+            (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
+           ((ctx->ext_attr_ver == 2) &&
+            (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
                if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
                        goto clear_extattr;
        }
+
        if (header->h_blocks != 1) {
                if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
                        goto clear_extattr;
@@ -1080,29 +1460,52 @@ 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;
        }
-       
+
        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 (entry->e_name_len == 0 || entry->e_name_index != 0) {
+               if ((ctx->ext_attr_ver == 1 &&
+                    (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
+                   (ctx->ext_attr_ver == 2 &&
+                    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))
                                goto clear_extattr;
                }
+               if (entry->e_value_offs + entry->e_value_size > fs->blocksize) {
+                       if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
+                               goto clear_extattr;
+                       break;
+               }
                if (entry->e_value_size &&
                    region_allocate(region, entry->e_value_offs,
                                    EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
                        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)) {
@@ -1116,15 +1519,229 @@ static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
                ea_refcount_store(ctx->refcount, blk, count);
        mark_block_used(ctx, blk);
        ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk);
-       
        return 1;
 
 clear_extattr:
+       if (region)
+               region_free(region);
        inode->i_file_acl = 0;
        e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
        return 0;
 }
 
+/* Returns 1 if bad htree, 0 if OK */
+static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
+                       ext2_ino_t ino, struct ext2_inode *inode,
+                       char *block_buf)
+{
+       struct ext2_dx_root_info        *root;
+       ext2_filsys                     fs = ctx->fs;
+       errcode_t                       retval;
+       blk_t                           blk;
+
+       if ((!LINUX_S_ISDIR(inode->i_mode) &&
+            fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
+           (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
+            fix_problem(ctx, PR_1_HTREE_SET, 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);
+
+       if ((root->reserved_zero || root->info_length < 8) &&
+           fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
+               return 1;
+
+       pctx->num = root->hash_version;
+       if ((root->hash_version != EXT2_HASH_LEGACY) &&
+           (root->hash_version != EXT2_HASH_HALF_MD4) &&
+           (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;
+
+       pctx->num = root->indirect_levels;
+       if ((root->indirect_levels > 1) &&
+           fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
+               return 1;
+       
+       return 0;
+}
+
+void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
+                       struct ext2_inode *inode, int restart_flag,
+                       const char *source)
+{
+       inode->i_flags = 0;
+       inode->i_links_count = 0;
+       ext2fs_icount_store(ctx->inode_link_info, ino, 0);
+       inode->i_dtime = ctx->now;
+
+       ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
+       ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
+       if (ctx->inode_reg_map)
+               ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
+       if (ctx->inode_bad_map)
+               ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
+
+       /*
+        * If the inode was partially accounted for before processing
+        * was aborted, we need to restart the pass 1 scan.
+        */
+       ctx->flags |= restart_flag;
+
+       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) {
+                       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) {
+                                       fix_problem(ctx,
+                                                   PR_1_EXTENT_DELETE_FAIL,
+                                                   pctx);
+                                       /* Should never get here */
+                                       ctx->flags |= E2F_FLAG_ABORT;
+                                       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) {
+                       mark_block_used(ctx, extent.e_pblk);
+                       pb->num_blocks++;
+                       pctx->errcode = ext2fs_extent_get(ehandle,
+                                                 EXT2_EXTENT_DOWN, &extent);
+                       if (pctx->errcode) {
+                               printf("Error1: %s on inode %u\n",
+                                       error_message(pctx->errcode), pctx->ino);
+                               abort();
+                       }
+                       scan_extent_node(ctx, pctx, pb, extent.e_lblk, ehandle);
+                       pctx->errcode = ext2fs_extent_get(ehandle,
+                                                 EXT2_EXTENT_UP, &extent);
+                       if (pctx->errcode) {
+                               printf("Error1: %s on inode %u\n",
+                                       error_message(pctx->errcode), pctx->ino);
+                               abort();
+                       }
+                       goto next;
+               }
+
+               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;
+               start_block = pb->last_block = extent.e_lblk + extent.e_len - 1;
+       next:
+               pctx->errcode = ext2fs_extent_get(ehandle,
+                                                 EXT2_EXTENT_NEXT_SIB,
+                                                 &extent);
+       }
+       if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
+               pctx->errcode = 0;
+}
+
+static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
+                                struct process_block_struct *pb)
+{
+       struct ext2_inode       *inode = pctx->inode;
+       ext2_extent_handle_t    ehandle;
+       ext2_filsys             fs = ctx->fs;
+       ext2_ino_t              ino = pctx->ino;
+
+       pctx->errcode = ext2fs_extent_open(fs, ino, &ehandle);
+       if (pctx->errcode &&
+           fix_problem(ctx, PR_1_READ_EXTENT, pctx)) {
+               e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks_extents");
+               pctx->errcode = 0;
+               return;
+       }
+
+       scan_extent_node(ctx, pctx, pb, 0, ehandle);
+
+       ext2fs_extent_free(ehandle);
+}
 
 /*
  * This subroutine is called on each inode to account for all of the
@@ -1138,13 +1755,13 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        ext2_ino_t      ino = pctx->ino;
        struct ext2_inode *inode = pctx->inode;
        int             bad_size = 0;
+       int             dirty_inode = 0;
+       int             extent_fs;
        __u64           size;
        
-       if (!ext2fs_inode_has_valid_blocks(inode))
-               return;
-       
        pb.ino = ino;
-       pb.num_blocks = pb.last_block = 0;
+       pb.num_blocks = 0;
+       pb.last_block = -1;
        pb.num_illegal_blocks = 0;
        pb.suppress = 0; pb.clear = 0;
        pb.fragmented = 0;
@@ -1157,6 +1774,10 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
        pb.pctx = pctx;
        pb.ctx = ctx;
        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 &
@@ -1165,19 +1786,29 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                else {
                        if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
                                inode->i_flags &= ~EXT2_COMPRBLK_FL;
-                               e2fsck_write_inode(ctx, ino, inode,
-                                                  "check_blocks");
+                               dirty_inode++;
                        }
                }
        }
 
-       pctx->errcode = ext2fs_block_iterate2(fs, ino,
-                                      pb.is_dir ? BLOCK_FLAG_HOLE : 0,
-                                      block_buf, process_block, &pb);
-       if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
-               return;
+       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)) {
+               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)
+               goto out;
        if (pctx->errcode)
                fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
 
@@ -1185,47 +1816,68 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                ctx->fs_fragmented++;
 
        if (pb.clear) {
-               e2fsck_read_inode(ctx, ino, inode, "check_blocks");
-               inode->i_links_count = 0;
-               ext2fs_icount_store(ctx->inode_link_info, ino, 0);
-               inode->i_dtime = time(0);
-               e2fsck_write_inode(ctx, ino, inode, "check_blocks");
-               ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
-               ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
-               ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
-               /*
-                * The inode was probably partially accounted for
-                * before processing was aborted, so we need to
-                * restart the pass 1 scan.
-                */
-               ctx->flags |= E2F_FLAG_RESTART;
+               e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
+                                  "check_blocks");
                return;
        }
-       if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf))
-               pb.num_blocks++;
+       
+       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;
+               }
+       }
 
-       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,
-              pb.num_blocks);
+       if (inode->i_flags & EXT2_INDEX_FL) {
+               if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
+                       inode->i_flags &= ~EXT2_INDEX_FL;
+                       dirty_inode++;
+               } else {
+#ifdef ENABLE_HTREE
+                       e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
 #endif
+               }
+       }
+       if (ctx->dirs_to_hash && pb.is_dir &&
+           !(inode->i_flags & EXT2_INDEX_FL) &&
+           ((inode->i_size / fs->blocksize) >= 3))
+               ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
+               
        if (!pb.num_blocks && pb.is_dir) {
                if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
-                       inode->i_links_count = 0;
-                       ext2fs_icount_store(ctx->inode_link_info, ino, 0);
-                       inode->i_dtime = time(0);
-                       e2fsck_write_inode(ctx, ino, inode, "check_blocks");
-                       ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
-                       ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
-                       ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
+                       e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
                        ctx->fs_directory_count--;
-                       pb.is_dir = 0;
+                       return;
                }
        }
+
+       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,
+              pb.num_blocks);
+#endif
        if (pb.is_dir) {
                int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
-               if (nblock > (pb.last_block + 1))
+               if (inode->i_size & (fs->blocksize - 1)) 
+                       bad_size = 5;
+               else if (nblock > (pb.last_block + 1))
                        bad_size = 1;
                else if (nblock < (pb.last_block + 1)) {
                        if (((pb.last_block + 1) - nblock) >
@@ -1233,35 +1885,54 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
                                bad_size = 2;
                }
        } else {
-               if (!LINUX_S_ISREG(inode->i_mode) && inode->i_size_high)
-                       bad_size = 5;
-               size = inode->i_size | ((__u64) inode->i_size_high << 32);
-               if ((size < pb.last_block * fs->blocksize))
+               e2_blkcnt_t blkpg = ctx->blocks_per_page;
+
+               size = EXT2_I_SIZE(inode);
+               if ((pb.last_block >= 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))
                        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 > (1LL << (32 + fs->super->s_log_block_size) - 1))
+                       /* too big for an extent-based file - 32bit ee_block */
+                       bad_size = 6;
        }
-       if (bad_size) {
+       /* i_size for symlinks is checked elsewhere */
+       if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
                pctx->num = (pb.last_block+1) * fs->blocksize;
                if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
                        inode->i_size = pctx->num;
-                       if (LINUX_S_ISREG(inode->i_mode))
+                       if (!LINUX_S_ISDIR(inode->i_mode))
                                inode->i_size_high = pctx->num >> 32;
-                       e2fsck_write_inode(ctx, ino, inode, "check_blocks");
+                       dirty_inode++;
                }
                pctx->num = 0;
        }
        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;
-                       e2fsck_write_inode(ctx, ino, inode, "check_blocks");
+                       inode->osd2.linux2.l_i_blocks_hi = 0;
+                       dirty_inode++;
                }
                pctx->num = 0;
        }
+out:
+       if (dirty_inode)
+               e2fsck_write_inode(ctx, ino, inode, "check_blocks");
 }
 
 #if 0
@@ -1322,8 +1993,8 @@ static char *describe_illegal_block(ext2_filsys fs, blk_t block)
 static int process_block(ext2_filsys fs,
                  blk_t *block_nr,
                  e2_blkcnt_t blockcnt,
-                 blk_t ref_block,
-                 int ref_offset
+                 blk_t ref_block EXT2FS_ATTR((unused)),
+                 int ref_offset EXT2FS_ATTR((unused)),
                  void *priv_data)
 {
        struct process_block_struct *p;
@@ -1379,6 +2050,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;
@@ -1399,8 +2071,8 @@ static int process_block(ext2_filsys fs,
                        p->fragmented = 1;
        }
        p->previous_block = blk;
-       
-       if (p->is_dir && blockcnt > 2*1024*1024/fs->blocksize)
+
+       if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
                problem = PR_1_TOOBIG_DIR;
        if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
                problem = PR_1_TOOBIG_REG;
@@ -1434,7 +2106,18 @@ static int process_block(ext2_filsys fs,
                        return 0;
        }
 
-       mark_block_used(ctx, blk);
+       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
+                * being in use; all of the other blocks are handled
+                * by mark_table_blocks()).
+                */
+               if (blockcnt == BLOCK_COUNT_DIND)
+                       mark_block_used(ctx, blk);
+       } else
+               mark_block_used(ctx, blk);
        p->num_blocks++;
        if (blockcnt >= 0)
                p->last_block = blockcnt;
@@ -1454,29 +2137,17 @@ mark_dir:
        return ret_code;
 }
 
-static void bad_block_indirect(e2fsck_t ctx, blk_t blk)
-{
-       struct problem_context pctx;
-
-       clear_problem_context(&pctx);
-       /*
-        * Prompt to see if we should continue or not.
-        */
-       if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, &pctx))
-               ctx->flags |= E2F_FLAG_ABORT;
-}
-
 static int process_bad_block(ext2_filsys fs,
                      blk_t *block_nr,
                      e2_blkcnt_t blockcnt,
-                     blk_t ref_block,
-                     int ref_offset,
+                     blk_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;
-       int             first_block;
-       int             i;
+       blk_t           first_block;
+       dgrp_t          i;
        struct problem_context *pctx;
        e2fsck_t        ctx;
 
@@ -1506,8 +2177,20 @@ static int process_bad_block(ext2_filsys fs,
        }
 
        if (blockcnt < 0) {
-               if (ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
-                       bad_block_indirect(ctx, blk);
+               if (ext2fs_test_block_bitmap(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, 
+                                                   blk)) {
+                       p->bbcheck = 1;
+                       if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, 
+                                       pctx)) {
+                               *block_nr = 0;
+                               return BLOCK_CHANGED;
+                       }
                        if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
                                return BLOCK_ABORT;
                } else
@@ -1598,8 +2281,13 @@ static int process_bad_block(ext2_filsys fs,
         * is using a bad block.
         */
        if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
-           p->inode->i_block[EXT2_DIND_BLOCK]) {
-               bad_block_indirect(ctx, blk);
+           (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
+           (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
+               p->bbcheck = 1;
+               if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
+                       *block_nr = 0;
+                       return BLOCK_CHANGED;
+               }
                if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
                        return BLOCK_ABORT;
                return 0;
@@ -1618,6 +2306,7 @@ static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
 {
        ext2_filsys fs = ctx->fs;
        blk_t           old_block = *new_block;
+       blk_t           last_block;
        int             i;
        char            *buf;
        struct problem_context  pctx;
@@ -1628,8 +2317,8 @@ static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
        pctx.blk = old_block;
        pctx.str = name;
 
-       pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
-                       first_block + fs->super->s_blocks_per_group,
+       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);
        if (pctx.errcode) {
                pctx.num = num;
@@ -1637,13 +2326,14 @@ static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
                ext2fs_unmark_valid(fs);
                return;
        }
-       pctx.errcode = ext2fs_get_mem(fs->blocksize, (void **) &buf);
+       pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
        if (pctx.errcode) {
                fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
                ext2fs_unmark_valid(fs);
                return;
        }
        ext2fs_mark_super_dirty(fs);
+       fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
        pctx.blk2 = *new_block;
        fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
                          PR_1_RELOC_TO), &pctx);
@@ -1665,7 +2355,7 @@ static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
                if (pctx.errcode)
                        fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
        }
-       ext2fs_free_mem((void **) &buf);
+       ext2fs_free_mem(&buf);
 }
 
 /*
@@ -1678,10 +2368,12 @@ static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
 static void handle_fs_bad_blocks(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       int             i;
-       int             first_block = fs->super->s_first_data_block;
+       dgrp_t          i;
+       blk_t           first_block;
 
        for (i = 0; i < fs->group_desc_count; i++) {
+               first_block = ext2fs_group_first_block(fs, i);
+
                if (ctx->invalid_block_bitmap_flag[i]) {
                        new_table_block(ctx, first_block, i, _("block bitmap"),
                                        1, &fs->group_desc[i].bg_block_bitmap);
@@ -1696,7 +2388,6 @@ static void handle_fs_bad_blocks(e2fsck_t ctx)
                                        &fs->group_desc[i].bg_inode_table);
                        ctx->flags |= E2F_FLAG_RESTART;
                }
-               first_block += fs->super->s_blocks_per_group;
        }
        ctx->invalid_bitmaps = 0;
 }
@@ -1708,31 +2399,18 @@ static void handle_fs_bad_blocks(e2fsck_t ctx)
 static void mark_table_blocks(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       blk_t   block, b;
-       int     i,j;
+       blk_t   b;
+       dgrp_t  i;
+       int     j;
        struct problem_context pctx;
        
        clear_problem_context(&pctx);
        
-       block = fs->super->s_first_data_block;
        for (i = 0; i < fs->group_desc_count; i++) {
                pctx.group = i;
 
-               if (ext2fs_bg_has_super(fs, i)) {
-                       /*
-                        * Mark this group's copy of the superblock
-                        */
-                       ext2fs_mark_block_bitmap(ctx->block_found_map, block);
-               
-                       /*
-                        * Mark this group's copy of the descriptors
-                        */
-                       for (j = 0; j < fs->desc_blocks; j++) {
-                               ext2fs_mark_block_bitmap(ctx->block_found_map,
-                                                        block + j + 1);
-                       }
-               }
-               
+               ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
+
                /*
                 * Mark the blocks used for the inode table
                 */
@@ -1788,7 +2466,6 @@ static void mark_table_blocks(e2fsck_t ctx)
                                     fs->group_desc[i].bg_inode_bitmap);
                        }
                }
-               block += fs->super->s_blocks_per_group;
        }
 }
        
@@ -1828,7 +2505,8 @@ static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
 {
        e2fsck_t ctx = (e2fsck_t) fs->priv_data;
 
-       if ((ino == ctx->stashed_ino) && ctx->stashed_inode)
+       if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
+               (inode != ctx->stashed_inode))
                *ctx->stashed_inode = *inode;
        return EXT2_ET_CALLBACK_NOTHANDLED;
 }
@@ -1845,6 +2523,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;
@@ -1855,6 +2575,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;