Whamcloud - gitweb
blkid: fix ZFS device detection
[tools/e2fsprogs.git] / e2fsck / pass3.c
index 72c4ec4..08e0b8d 100644 (file)
@@ -7,14 +7,14 @@
  * This file may be redistributed under the terms of the GNU Public
  * License.
  * %End-Header%
- * 
+ *
  * Pass #3 assures that all directories are connected to the
  * filesystem tree, using the following algorithm:
  *
  * First, the root directory is checked to make sure it exists; if
  * not, e2fsck will offer to create a new one.  It is then marked as
  * "done".
- * 
+ *
  * Then, pass3 interates over all directory inodes; for each directory
  * it attempts to trace up the filesystem tree, using dirinfo.parent
  * until it reaches a directory which has been marked "done".  If it
@@ -24,7 +24,7 @@
  * twice, then it has detected a filesystem loop, and it will again
  * offer to reconnect the directory to /lost+found in to break the
  * filesystem loop.
- * 
+ *
  * Pass 3 also contains the subroutine, e2fsck_reconnect_file() to
  * reconnect inodes to /lost+found; this subroutine is also used by
  * pass 4.  e2fsck_reconnect_file() calls get_lost_and_found(), which
@@ -34,6 +34,7 @@
  *             - The dirinfo directory information cache.
  */
 
+#include "config.h"
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
 #endif
 #include "problem.h"
 
 static void check_root(e2fsck_t ctx);
-static int check_directory(e2fsck_t ctx, struct dir_info *dir,
+static int check_directory(e2fsck_t ctx, ext2_ino_t ino,
                           struct problem_context *pctx);
-static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent);
+static void fix_dotdot(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent);
 
 static ext2fs_inode_bitmap inode_loop_detect = 0;
 static ext2fs_inode_bitmap inode_done_map = 0;
-       
+
 void e2fsck_pass3(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       int             i;
+       struct dir_info_iter *iter = NULL;
 #ifdef RESOURCE_TRACK
        struct resource_track   rtrack;
 #endif
@@ -60,10 +61,7 @@ void e2fsck_pass3(e2fsck_t ctx)
        struct dir_info *dir;
        unsigned long maxdirs, count;
 
-#ifdef RESOURCE_TRACK
-       init_resource_track(&rtrack);
-#endif
-
+       init_resource_track(&rtrack, ctx->fs->io);
        clear_problem_context(&pctx);
 
 #ifdef MTRACE
@@ -76,26 +74,22 @@ void e2fsck_pass3(e2fsck_t ctx)
        /*
         * Allocate some bitmaps to do loop detection.
         */
-       pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("inode done bitmap"),
-                                                   &inode_done_map);
+       pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("inode done bitmap"),
+                                       EXT2FS_BMAP64_AUTODIR,
+                                       "inode_done_map", &inode_done_map);
        if (pctx.errcode) {
                pctx.num = 2;
                fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                goto abort_exit;
        }
-#ifdef RESOURCE_TRACK
-       if (ctx->options & E2F_OPT_TIME) {
-               e2fsck_clear_progbar(ctx);
-               print_resource_track(_("Peak memory"), &ctx->global_rtrack);
-       }
-#endif
+       print_resource_track(ctx, _("Peak memory"), &ctx->global_rtrack, NULL);
 
        check_root(ctx);
        if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
                goto abort_exit;
 
-       ext2fs_mark_inode_bitmap(inode_done_map, EXT2_ROOT_INO);
+       ext2fs_mark_inode_bitmap2(inode_done_map, EXT2_ROOT_INO);
 
        maxdirs = e2fsck_get_num_dirinfo(ctx);
        count = 1;
@@ -103,21 +97,22 @@ void e2fsck_pass3(e2fsck_t ctx)
        if (ctx->progress)
                if ((ctx->progress)(ctx, 3, 0, maxdirs))
                        goto abort_exit;
-       
-       for (i=0; (dir = e2fsck_dir_info_iter(ctx, &i)) != 0;) {
+
+       iter = e2fsck_dir_info_iter_begin(ctx);
+       while ((dir = e2fsck_dir_info_iter(ctx, iter)) != 0) {
                if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
                        goto abort_exit;
                if (ctx->progress && (ctx->progress)(ctx, 3, count++, maxdirs))
                        goto abort_exit;
-               if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino))
-                       if (check_directory(ctx, dir, &pctx))
+               if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dir->ino))
+                       if (check_directory(ctx, dir->ino, &pctx))
                                goto abort_exit;
        }
 
        /*
         * Force the creation of /lost+found if not present
         */
-       if ((ctx->flags & E2F_OPT_READONLY) == 0)
+       if ((ctx->options & E2F_OPT_READONLY) == 0)
                e2fsck_get_lost_and_found(ctx, 1);
 
        /*
@@ -125,8 +120,10 @@ void e2fsck_pass3(e2fsck_t ctx)
         * optimized, do it here.
         */
        e2fsck_rehash_directories(ctx);
-       
+
 abort_exit:
+       if (iter)
+               e2fsck_dir_info_iter_end(ctx, iter);
        e2fsck_free_dir_info(ctx);
        if (inode_loop_detect) {
                ext2fs_free_inode_bitmap(inode_loop_detect);
@@ -137,12 +134,18 @@ abort_exit:
                inode_done_map = 0;
        }
 
-#ifdef RESOURCE_TRACK
-       if (ctx->options & E2F_OPT_TIME2) {
-               e2fsck_clear_progbar(ctx);
-               print_resource_track(_("Pass 3"), &rtrack);
+       if (ctx->lnf_repair_block) {
+               ext2fs_unmark_block_bitmap2(ctx->block_found_map,
+                                           ctx->lnf_repair_block);
+               ctx->lnf_repair_block = 0;
        }
-#endif
+       if (ctx->root_repair_block) {
+               ext2fs_unmark_block_bitmap2(ctx->block_found_map,
+                                           ctx->root_repair_block);
+               ctx->root_repair_block = 0;
+       }
+
+       print_resource_track(ctx, _("Pass 3"), &rtrack, ctx->fs->io);
 }
 
 /*
@@ -152,20 +155,20 @@ abort_exit:
 static void check_root(e2fsck_t ctx)
 {
        ext2_filsys fs = ctx->fs;
-       blk_t                   blk;
+       blk64_t                 blk;
        struct ext2_inode       inode;
        char *                  block;
        struct problem_context  pctx;
-       
+
        clear_problem_context(&pctx);
-       
-       if (ext2fs_test_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO)) {
+
+       if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO)) {
                /*
                 * If the root inode is not a directory, die here.  The
                 * user must have answered 'no' in pass1 when we
                 * offered to clear it.
                 */
-               if (!(ext2fs_test_inode_bitmap(ctx->inode_dir_map,
+               if (!(ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
                                               EXT2_ROOT_INO))) {
                        fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
                        ctx->flags |= E2F_FLAG_ABORT;
@@ -180,19 +183,25 @@ static void check_root(e2fsck_t ctx)
        }
 
        e2fsck_read_bitmaps(ctx);
-       
+
        /*
         * First, find a free block
         */
-       pctx.errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
+       if (ctx->root_repair_block) {
+               blk = ctx->root_repair_block;
+               ctx->root_repair_block = 0;
+               goto skip_new_block;
+       }
+       pctx.errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
        if (pctx.errcode) {
                pctx.str = "ext2fs_new_block";
                fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
-       ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
-       ext2fs_mark_block_bitmap(fs->block_map, blk);
+       ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
+skip_new_block:
+       ext2fs_mark_block_bitmap2(fs->block_map, blk);
        ext2fs_mark_bb_dirty(fs);
 
        /*
@@ -207,9 +216,9 @@ static void check_root(e2fsck_t ctx)
                return;
        }
 
-       pctx.errcode = ext2fs_write_dir_block(fs, blk, block);
+       pctx.errcode = ext2fs_write_dir_block3(fs, blk, block, 0);
        if (pctx.errcode) {
-               pctx.str = "ext2fs_write_dir_block";
+               pctx.str = "ext2fs_write_dir_block3";
                fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
                ctx->flags |= E2F_FLAG_ABORT;
                return;
@@ -224,7 +233,7 @@ static void check_root(e2fsck_t ctx)
        inode.i_size = fs->blocksize;
        inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
        inode.i_links_count = 2;
-       inode.i_blocks = fs->blocksize / 512;
+       ext2fs_iblk_set(fs, &inode, 1);
        inode.i_block[0] = blk;
 
        /*
@@ -237,7 +246,7 @@ static void check_root(e2fsck_t ctx)
                ctx->flags |= E2F_FLAG_ABORT;
                return;
        }
-       
+
        /*
         * Miscellaneous bookkeeping...
         */
@@ -245,9 +254,9 @@ static void check_root(e2fsck_t ctx)
        ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
        ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
 
-       ext2fs_mark_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO);
-       ext2fs_mark_inode_bitmap(ctx->inode_dir_map, EXT2_ROOT_INO);
-       ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO);
+       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO);
+       ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, EXT2_ROOT_INO);
+       ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_ROOT_INO);
        ext2fs_mark_ib_dirty(fs);
 }
 
@@ -258,7 +267,7 @@ static void check_root(e2fsck_t ctx)
  * the lost+found.  We have to do loop detection; if we ever discover
  * a loop, we treat that as a disconnected directory and offer to
  * reparent it to lost+found.
- * 
+ *
  * However, loop detection is expensive, because for very large
  * filesystems, the inode_loop_detect bitmap is huge, and clearing it
  * is non-trivial.  Loops in filesystems are also a rare error case,
@@ -267,16 +276,13 @@ static void check_root(e2fsck_t ctx)
  * increment a counter; if the counter exceeds some extreme threshold,
  * then we try again with the loop detection bitmap enabled.
  */
-static int check_directory(e2fsck_t ctx, struct dir_info *dir,
+static int check_directory(e2fsck_t ctx, ext2_ino_t dir,
                           struct problem_context *pctx)
 {
        ext2_filsys     fs = ctx->fs;
-       struct dir_info *p = dir;
+       ext2_ino_t      ino = dir, parent;
        int             loop_pass = 0, parent_count = 0;
 
-       if (!p)
-               return 0;
-
        while (1) {
                /*
                 * Mark this inode as being "done"; by the time we
@@ -288,44 +294,38 @@ static int check_directory(e2fsck_t ctx, struct dir_info *dir,
                 * If it was marked done already, then we've reached a
                 * parent we've already checked.
                 */
-               if (ext2fs_mark_inode_bitmap(inode_done_map, p->ino))
+               if (ext2fs_mark_inode_bitmap2(inode_done_map, ino))
                        break;
 
+               if (e2fsck_dir_info_get_parent(ctx, ino, &parent)) {
+                       fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
+                       return 0;
+               }
+
                /*
                 * If this directory doesn't have a parent, or we've
                 * seen the parent once already, then offer to
                 * reparent it to lost+found
                 */
-               if (!p->parent ||
-                   (loop_pass && 
-                    (ext2fs_test_inode_bitmap(inode_loop_detect,
-                                             p->parent)))) {
-                       pctx->ino = p->ino;
+               if (!parent ||
+                   (loop_pass &&
+                    (ext2fs_test_inode_bitmap2(inode_loop_detect,
+                                              parent)))) {
+                       pctx->ino = ino;
                        if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
                                if (e2fsck_reconnect_file(ctx, pctx->ino))
                                        ext2fs_unmark_valid(fs);
                                else {
-                                       p = e2fsck_get_dir_info(ctx, pctx->ino);
-                                       if (!p) {
-                                               fix_problem(ctx, 
-                                                   PR_3_NO_DIRINFO, pctx);
-                                               return 0;
-                                       }
-                                       p->parent = ctx->lost_and_found;
-                                       fix_dotdot(ctx, p, ctx->lost_and_found);
+                                       fix_dotdot(ctx, pctx->ino,
+                                                  ctx->lost_and_found);
+                                       parent = ctx->lost_and_found;
                                }
                        }
                        break;
                }
-               p = e2fsck_get_dir_info(ctx, p->parent);
-               if (!p) {
-                       pctx->ino = p->parent;
-                       fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
-                       return 0;
-               }
+               ino = parent;
                if (loop_pass) {
-                       ext2fs_mark_inode_bitmap(inode_loop_detect,
-                                                p->ino);
+                       ext2fs_mark_inode_bitmap2(inode_loop_detect, ino);
                } else if (parent_count++ > 2048) {
                        /*
                         * If we've run into a path depth that's
@@ -337,16 +337,16 @@ static int check_directory(e2fsck_t ctx, struct dir_info *dir,
                        if (inode_loop_detect)
                                ext2fs_clear_inode_bitmap(inode_loop_detect);
                        else {
-                               pctx->errcode = ext2fs_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), &inode_loop_detect);
+                               pctx->errcode = e2fsck_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), EXT2FS_BMAP64_AUTODIR, "inode_loop_detect", &inode_loop_detect);
                                if (pctx->errcode) {
                                        pctx->num = 1;
-                                       fix_problem(ctx, 
+                                       fix_problem(ctx,
                                    PR_3_ALLOCATE_IBITMAP_ERROR, pctx);
                                        ctx->flags |= E2F_FLAG_ABORT;
                                        return -1;
                                }
                        }
-                       p = dir;
+                       ino = dir;
                }
        }
 
@@ -354,12 +354,15 @@ static int check_directory(e2fsck_t ctx, struct dir_info *dir,
         * Make sure that .. and the parent directory are the same;
         * offer to fix it if not.
         */
-       if (dir->parent != dir->dotdot) {
-               pctx->ino = dir->ino;
-               pctx->ino2 = dir->dotdot;
-               pctx->dir = dir->parent;
+       pctx->ino = dir;
+       if (e2fsck_dir_info_get_dotdot(ctx, dir, &pctx->ino2) ||
+           e2fsck_dir_info_get_parent(ctx, dir, &pctx->dir)) {
+               fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
+               return 0;
+       }
+       if (pctx->ino2 != pctx->dir) {
                if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
-                       fix_dotdot(ctx, dir, dir->parent);
+                       fix_dotdot(ctx, dir, pctx->dir);
        }
        return 0;
 }
@@ -372,29 +375,28 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
 {
        ext2_filsys fs = ctx->fs;
        ext2_ino_t                      ino;
-       blk_t                   blk;
+       blk64_t                 blk;
        errcode_t               retval;
        struct ext2_inode       inode;
        char *                  block;
        static const char       name[] = "lost+found";
        struct  problem_context pctx;
-       struct dir_info         *dirinfo;
 
        if (ctx->lost_and_found)
                return ctx->lost_and_found;
 
        clear_problem_context(&pctx);
-       
+
        retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
                               sizeof(name)-1, 0, &ino);
        if (retval && !fix)
                return 0;
        if (!retval) {
-               if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, ino)) {
+               if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino)) {
                        ctx->lost_and_found = ino;
                        return ino;
                }
-                               
+
                /* Lost+found isn't a directory! */
                if (!fix)
                        return 0;
@@ -409,9 +411,7 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
                        fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
                        return 0;
                }
-               dirinfo = e2fsck_get_dir_info(ctx, ino);
-               if (dirinfo)
-                       dirinfo->parent = 0;
+               (void) e2fsck_dir_info_set_parent(ctx, ino, 0);
                e2fsck_adjust_inode_count(ctx, ino, -1);
        } else if (retval != EXT2_ET_FILE_NOT_FOUND) {
                pctx.errcode = retval;
@@ -425,18 +425,24 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
         * them.
         */
        e2fsck_read_bitmaps(ctx);
-       
+
        /*
         * First, find a free block
         */
-       retval = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
+       if (ctx->lnf_repair_block) {
+               blk = ctx->lnf_repair_block;
+               ctx->lnf_repair_block = 0;
+               goto skip_new_block;
+       }
+       retval = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
        if (retval) {
                pctx.errcode = retval;
                fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
                return 0;
        }
-       ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
-       ext2fs_block_alloc_stats(fs, blk, +1);
+       ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
+skip_new_block:
+       ext2fs_block_alloc_stats2(fs, blk, +1);
 
        /*
         * Next find a free inode.
@@ -448,8 +454,8 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
                fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
                return 0;
        }
-       ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
-       ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
+       ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
+       ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
        ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
 
        /*
@@ -462,7 +468,7 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
                return 0;
        }
 
-       retval = ext2fs_write_dir_block(fs, blk, block);
+       retval = ext2fs_write_dir_block3(fs, blk, block, 0);
        ext2fs_free_mem(&block);
        if (retval) {
                pctx.errcode = retval;
@@ -478,7 +484,7 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
        inode.i_size = fs->blocksize;
        inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
        inode.i_links_count = 2;
-       inode.i_blocks = fs->blocksize / 512;
+       ext2fs_iblk_set(fs, &inode, 1);
        inode.i_block[0] = blk;
 
        /*
@@ -508,6 +514,8 @@ ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
        ext2fs_icount_store(ctx->inode_count, ino, 2);
        ext2fs_icount_store(ctx->inode_link_info, ino, 2);
        ctx->lost_and_found = ino;
+       quota_data_add(ctx->qctx, &inode, ino, fs->blocksize);
+       quota_data_inodes(ctx->qctx, &inode, ino, +1);
 #if 0
        printf("/lost+found created; inode #%lu\n", ino);
 #endif
@@ -537,7 +545,7 @@ int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t ino)
                fix_problem(ctx, PR_3_NO_LPF, &pctx);
                return 1;
        }
-       
+
        sprintf(name, "#%u", ino);
        if (ext2fs_read_inode(fs, ino, &inode) == 0)
                file_type = ext2_file_type(inode.i_mode);
@@ -545,7 +553,7 @@ int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t ino)
        if (retval == EXT2_ET_DIR_NO_SPACE) {
                if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
                        return 1;
-               retval = e2fsck_expand_directory(ctx, ctx->lost_and_found, 
+               retval = e2fsck_expand_directory(ctx, ctx->lost_and_found,
                                                 1, 0);
                if (retval) {
                        pctx.errcode = retval;
@@ -573,7 +581,7 @@ errcode_t e2fsck_adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj)
        ext2_filsys fs = ctx->fs;
        errcode_t               retval;
        struct ext2_inode       inode;
-       
+
        if (!ino)
                return 0;
 
@@ -599,7 +607,7 @@ errcode_t e2fsck_adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj)
                ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
                inode.i_links_count--;
        }
-       
+
        retval = ext2fs_write_inode(fs, ino, &inode);
        if (retval)
                return retval;
@@ -633,7 +641,7 @@ static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
                return 0;
 
        clear_problem_context(&pctx);
-       
+
        retval = e2fsck_adjust_inode_count(fp->ctx, dirent->inode, -1);
        if (retval) {
                pctx.errcode = retval;
@@ -647,7 +655,7 @@ static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
        dirent->inode = fp->parent;
        if (fp->ctx->fs->super->s_feature_incompat &
            EXT2_FEATURE_INCOMPAT_FILETYPE)
-               dirent->name_len = (dirent->name_len & 0xFF) | 
+               dirent->name_len = (dirent->name_len & 0xFF) |
                        (EXT2_FT_DIR << 8);
        else
                dirent->name_len = dirent->name_len & 0xFF;
@@ -656,7 +664,7 @@ static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
        return DIRENT_ABORT | DIRENT_CHANGED;
 }
 
-static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent)
+static void fix_dotdot(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
 {
        ext2_filsys fs = ctx->fs;
        errcode_t       retval;
@@ -669,21 +677,23 @@ static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent)
        fp.ctx = ctx;
 
 #if 0
-       printf("Fixing '..' of inode %lu to be %lu...\n", dir->ino, parent);
+       printf("Fixing '..' of inode %lu to be %lu...\n", ino, parent);
 #endif
-       
-       retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY,
+
+       clear_problem_context(&pctx);
+       pctx.ino = ino;
+       retval = ext2fs_dir_iterate(fs, ino, DIRENT_FLAG_INCLUDE_EMPTY,
                                    0, fix_dotdot_proc, &fp);
        if (retval || !fp.done) {
-               clear_problem_context(&pctx);
-               pctx.ino = dir->ino;
                pctx.errcode = retval;
                fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
                            PR_3_FIX_PARENT_NOFIND, &pctx);
                ext2fs_unmark_valid(fs);
        }
-       dir->dotdot = parent;
-       
+       (void) e2fsck_dir_info_set_dotdot(ctx, ino, parent);
+       if (e2fsck_dir_info_set_parent(ctx, ino, ctx->lost_and_found))
+               fix_problem(ctx, PR_3_NO_DIRINFO, &pctx);
+
        return;
 }
 
@@ -693,30 +703,30 @@ static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent)
  */
 
 struct expand_dir_struct {
-       int                     num;
-       int                     guaranteed_size;
-       int                     newblocks;
-       int                     last_block;
+       blk64_t                 num;
+       e2_blkcnt_t             guaranteed_size;
+       blk64_t                 newblocks;
+       blk64_t                 last_block;
        errcode_t               err;
        e2fsck_t                ctx;
 };
 
 static int expand_dir_proc(ext2_filsys fs,
-                          blk_t        *blocknr,
+                          blk64_t      *blocknr,
                           e2_blkcnt_t  blockcnt,
-                          blk_t ref_block EXT2FS_ATTR((unused)),
+                          blk64_t ref_block EXT2FS_ATTR((unused)),
                           int ref_offset EXT2FS_ATTR((unused)),
                           void *priv_data)
 {
        struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
-       blk_t   new_blk;
-       static blk_t    last_blk = 0;
+       blk64_t new_blk;
+       static blk64_t  last_blk = 0;
        char            *block;
        errcode_t       retval;
        e2fsck_t        ctx;
 
        ctx = es->ctx;
-       
+
        if (es->guaranteed_size && blockcnt >= es->guaranteed_size)
                return BLOCK_ABORT;
 
@@ -726,12 +736,23 @@ static int expand_dir_proc(ext2_filsys fs,
                last_blk = *blocknr;
                return 0;
        }
-       retval = ext2fs_new_block(fs, last_blk, ctx->block_found_map,
-                                 &new_blk);
-       if (retval) {
-               es->err = retval;
-               return BLOCK_ABORT;
+
+       if (blockcnt &&
+           (EXT2FS_B2C(fs, last_blk) == EXT2FS_B2C(fs, last_blk + 1)))
+               new_blk = last_blk + 1;
+       else {
+               last_blk &= ~EXT2FS_CLUSTER_MASK(fs);
+               retval = ext2fs_new_block2(fs, last_blk, ctx->block_found_map,
+                                         &new_blk);
+               if (retval) {
+                       es->err = retval;
+                       return BLOCK_ABORT;
+               }
+               es->newblocks++;
+               ext2fs_block_alloc_stats2(fs, new_blk, +1);
        }
+       last_blk = new_blk;
+
        if (blockcnt > 0) {
                retval = ext2fs_new_dir_block(fs, 0, 0, &block);
                if (retval) {
@@ -739,7 +760,7 @@ static int expand_dir_proc(ext2_filsys fs,
                        return BLOCK_ABORT;
                }
                es->num--;
-               retval = ext2fs_write_dir_block(fs, new_blk, block);
+               retval = ext2fs_write_dir_block3(fs, new_blk, block, 0);
        } else {
                retval = ext2fs_get_mem(fs->blocksize, &block);
                if (retval) {
@@ -747,18 +768,16 @@ static int expand_dir_proc(ext2_filsys fs,
                        return BLOCK_ABORT;
                }
                memset(block, 0, fs->blocksize);
-               retval = io_channel_write_blk(fs->io, new_blk, 1, block);
-       }       
+               retval = io_channel_write_blk64(fs->io, new_blk, 1, block);
+       }
        if (retval) {
                es->err = retval;
                return BLOCK_ABORT;
        }
        ext2fs_free_mem(&block);
        *blocknr = new_blk;
-       ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk);
-       ext2fs_block_alloc_stats(fs, new_blk, +1);
-       es->newblocks++;
-       
+       ext2fs_mark_block_bitmap2(ctx->block_found_map, new_blk);
+
        if (es->num == 0)
                return (BLOCK_CHANGED | BLOCK_ABORT);
        else
@@ -772,7 +791,8 @@ errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
        errcode_t       retval;
        struct expand_dir_struct es;
        struct ext2_inode       inode;
-       
+       blk64_t         sz;
+
        if (!(fs->flags & EXT2_FLAG_RW))
                return EXT2_ET_RO_FILSYS;
 
@@ -785,15 +805,15 @@ errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
        retval = ext2fs_check_directory(fs, dir);
        if (retval)
                return retval;
-       
+
        es.num = num;
        es.guaranteed_size = guaranteed_size;
        es.last_block = 0;
        es.err = 0;
        es.newblocks = 0;
        es.ctx = ctx;
-       
-       retval = ext2fs_block_iterate2(fs, dir, BLOCK_FLAG_APPEND,
+
+       retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_APPEND,
                                       0, expand_dir_proc, &es);
 
        if (es.err)
@@ -805,9 +825,13 @@ errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
        retval = ext2fs_read_inode(fs, dir, &inode);
        if (retval)
                return retval;
-       
-       inode.i_size = (es.last_block + 1) * fs->blocksize;
-       inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
+
+       sz = (es.last_block + 1) * fs->blocksize;
+       retval = ext2fs_inode_size_set(fs, &inode, sz);
+       if (retval)
+               return retval;
+       ext2fs_iblk_add_blocks(fs, &inode, es.newblocks);
+       quota_data_add(ctx->qctx, &inode, dir, es.newblocks * fs->blocksize);
 
        e2fsck_write_inode(ctx, dir, &inode, "expand_directory");