Whamcloud - gitweb
ChangeLog, jfs_compat.h, journal.c, pass3.c:
[tools/e2fsprogs.git] / e2fsck / pass3.c
index 560f291..9344e55 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * pass3.c -- pass #3 of e2fsck: Check for directory connectivity
  *
- * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
+ * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o.
  *
  * %Begin-Header%
  * This file may be redistributed under the terms of the GNU Public
@@ -42,8 +42,8 @@
 #include "problem.h"
 
 static void check_root(e2fsck_t ctx);
-static void check_directory(e2fsck_t ctx, struct dir_info *dir,
-                           struct problem_context *pctx);
+static int check_directory(e2fsck_t ctx, struct dir_info *dir,
+                          struct problem_context *pctx);
 static ino_t get_lost_and_found(e2fsck_t ctx);
 static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ino_t parent);
 static errcode_t adjust_inode_count(e2fsck_t ctx, ino_t ino, int adj);
@@ -82,15 +82,7 @@ void e2fsck_pass3(e2fsck_t ctx)
        /*
         * Allocate some bitmaps to do loop detection.
         */
-       pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
-                   "inode loop detection bitmap", &inode_loop_detect);
-       if (pctx.errcode) {
-               pctx.num = 1;
-               fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
-               ctx->flags |= E2F_FLAG_ABORT;
-               goto abort_exit;
-       }
-       pctx.errcode = ext2fs_allocate_inode_bitmap(fs, "inode done bitmap",
+       pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("inode done bitmap"),
                                                    &inode_done_map);
        if (pctx.errcode) {
                pctx.num = 2;
@@ -99,8 +91,10 @@ void e2fsck_pass3(e2fsck_t ctx)
                goto abort_exit;
        }
 #ifdef RESOURCE_TRACK
-       if (ctx->options & E2F_OPT_TIME)
-               print_resource_track("Peak memory", &ctx->global_rtrack);
+       if (ctx->options & E2F_OPT_TIME) {
+               e2fsck_clear_progbar(ctx);
+               print_resource_track(_("Peak memory"), &ctx->global_rtrack);
+       }
 #endif
 
        check_root(ctx);
@@ -121,7 +115,8 @@ void e2fsck_pass3(e2fsck_t ctx)
                        if ((ctx->progress)(ctx, 3, count++, maxdirs))
                                goto abort_exit;
                if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino))
-                       check_directory(ctx, dir, &pctx);
+                       if (check_directory(ctx, dir, &pctx))
+                               goto abort_exit;
        }
 
        /*
@@ -132,13 +127,19 @@ void e2fsck_pass3(e2fsck_t ctx)
 
 abort_exit:
        e2fsck_free_dir_info(ctx);
-       if (inode_loop_detect)
+       if (inode_loop_detect) {
                ext2fs_free_inode_bitmap(inode_loop_detect);
-       if (inode_done_map)
+               inode_loop_detect = 0;
+       }
+       if (inode_done_map) {
                ext2fs_free_inode_bitmap(inode_done_map);
+               inode_done_map = 0;
+       }
 #ifdef RESOURCE_TRACK
-       if (ctx->options & E2F_OPT_TIME2)
-               print_resource_track("Pass 3", &rtrack);
+       if (ctx->options & E2F_OPT_TIME2) {
+               e2fsck_clear_progbar(ctx);
+               print_resource_track(_("Pass 3"), &rtrack);
+       }
 #endif
 }
 
@@ -255,34 +256,38 @@ 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,
+ * and we shouldn't optimize for error cases.  So we try two passes of
+ * the algorithm.  The first time, we ignore loop detection and merely
+ * increment a counter; if the counter exceeds some extreme threshold,
+ * then we try again with the loop detection bitmap enabled.
  */
-static void check_directory(e2fsck_t ctx, struct dir_info *dir,
-                           struct problem_context *pctx)
+static int check_directory(e2fsck_t ctx, struct dir_info *dir,
+                          struct problem_context *pctx)
 {
-       ext2_filsys fs = ctx->fs;
+       ext2_filsys     fs = ctx->fs;
        struct dir_info *p = dir;
+       int             loop_pass = 0, parent_count = 0;
 
        if (!p)
-               return;
-
-       ext2fs_clear_inode_bitmap(inode_loop_detect);
+               return 0;
 
-       /*
-        * Keep going until we find a parent which we've already
-        * checked.  We know it's either already connected to the
-        * directory tree, or it isn't but the user has already told
-        * us he doesn't want us to reconnect the disconnected
-        * subtree.
-        */
-       while (!ext2fs_test_inode_bitmap(inode_done_map, p->ino)) {
+       while (1) {
                /*
                 * Mark this inode as being "done"; by the time we
                 * return from this function, the inode we either be
                 * verified as being connected to the directory tree,
                 * or we will have offered to reconnect this to
                 * lost+found.
+                *
+                * If it was marked done already, then we've reached a
+                * parent we've already checked.
                 */
-               ext2fs_mark_inode_bitmap(inode_done_map, p->ino);
+               if (ext2fs_mark_inode_bitmap(inode_done_map, p->ino))
+                       break;
 
                /*
                 * If this directory doesn't have a parent, or we've
@@ -290,8 +295,9 @@ static void check_directory(e2fsck_t ctx, struct dir_info *dir,
                 * reparent it to lost+found
                 */
                if (!p->parent ||
-                   (ext2fs_test_inode_bitmap(inode_loop_detect,
-                                             p->parent))) {
+                   (loop_pass && 
+                    (ext2fs_test_inode_bitmap(inode_loop_detect,
+                                             p->parent)))) {
                        pctx->ino = p->ino;
                        if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
                                if (e2fsck_reconnect_file(ctx, p->ino))
@@ -303,13 +309,35 @@ static void check_directory(e2fsck_t ctx, struct dir_info *dir,
                        }
                        break;
                }
-               ext2fs_mark_inode_bitmap(inode_loop_detect,
-                                        p->parent);
-               pctx->ino = p->parent;
                p = e2fsck_get_dir_info(ctx, p->parent);
                if (!p) {
                        fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
-                       return;
+                       return 0;
+               }
+               if (loop_pass) {
+                       ext2fs_mark_inode_bitmap(inode_loop_detect,
+                                                p->ino);
+               } else if (parent_count++ > 2048) {
+                       /*
+                        * If we've run into a path depth that's
+                        * greater than 2048, try again with the inode
+                        * loop bitmap turned on and start from the
+                        * top.
+                        */
+                       loop_pass = 1;
+                       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);
+                               if (pctx->errcode) {
+                                       pctx->num = 1;
+                                       fix_problem(ctx, 
+                                   PR_3_ALLOCATE_IBITMAP_ERROR, pctx);
+                                       ctx->flags |= E2F_FLAG_ABORT;
+                                       return -1;
+                               }
+                       }
+                       p = dir;
                }
        }
 
@@ -324,6 +352,7 @@ static void check_directory(e2fsck_t ctx, struct dir_info *dir,
                if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
                        fix_dotdot(ctx, dir, dir->parent);
        }
+       return 0;
 }      
 
 /*
@@ -340,14 +369,32 @@ ino_t get_lost_and_found(e2fsck_t ctx)
        char *                  block;
        const char              name[] = "lost+found";
        struct  problem_context pctx;
+       struct dir_info         *dirinfo;
 
        clear_problem_context(&pctx);
        
        retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
                               sizeof(name)-1, 0, &ino);
-       if (!retval)
-               return ino;
-       if (retval != EXT2_ET_FILE_NOT_FOUND) {
+       if (!retval) {
+               if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, ino))
+                       return ino;
+               /* Lost+found isn't a directory! */
+               pctx.ino = ino;
+               if (!fix_problem(ctx, PR_3_LPF_NOTDIR, &pctx))
+                       return 0;
+
+               /* OK, unlink the old /lost+found file. */
+               pctx.errcode = ext2fs_unlink(fs, EXT2_ROOT_INO, name, ino, 0);
+               if (pctx.errcode) {
+                       pctx.str = "ext2fs_unlink";
+                       fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
+                       return 0;
+               }
+               dirinfo = e2fsck_get_dir_info(ctx, ino);
+               if (dirinfo)
+                       dirinfo->parent = 0;
+               adjust_inode_count(ctx, ino, -1);
+       } else if (retval != EXT2_ET_FILE_NOT_FOUND) {
                pctx.errcode = retval;
                fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx);
        }
@@ -429,7 +476,7 @@ ino_t get_lost_and_found(e2fsck_t ctx)
        /*
         * Finally, create the directory link
         */
-       pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, 0);
+       pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, EXT2_FT_DIR);
        if (pctx.errcode) {
                pctx.str = "ext2fs_link";
                fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
@@ -452,15 +499,17 @@ ino_t get_lost_and_found(e2fsck_t ctx)
 /*
  * This routine will connect a file to lost+found
  */
-int e2fsck_reconnect_file(e2fsck_t ctx, ino_t inode)
+int e2fsck_reconnect_file(e2fsck_t ctx, ino_t ino)
 {
        ext2_filsys fs = ctx->fs;
        errcode_t       retval;
        char            name[80];
        struct problem_context  pctx;
+       struct ext2_inode       inode;
+       int             file_type = 0;
 
        clear_problem_context(&pctx);
-       pctx.ino = inode;
+       pctx.ino = ino;
 
        if (!bad_lost_and_found && !lost_and_found) {
                lost_and_found = get_lost_and_found(ctx);
@@ -472,8 +521,10 @@ int e2fsck_reconnect_file(e2fsck_t ctx, ino_t inode)
                return 1;
        }
        
-       sprintf(name, "#%lu", inode);
-       retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
+       sprintf(name, "#%lu", ino);
+       if (ext2fs_read_inode(fs, ino, &inode) == 0)
+               file_type = ext2_file_type(inode.i_mode);
+       retval = ext2fs_link(fs, lost_and_found, name, ino, file_type);
        if (retval == EXT2_ET_DIR_NO_SPACE) {
                if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
                        return 1;
@@ -483,14 +534,14 @@ int e2fsck_reconnect_file(e2fsck_t ctx, ino_t inode)
                        fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
                        return 1;
                }
-               retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
+               retval = ext2fs_link(fs, lost_and_found, name, ino, file_type);
        }
        if (retval) {
                pctx.errcode = retval;
                fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
                return 1;
        }
-       adjust_inode_count(ctx, inode, +1);
+       adjust_inode_count(ctx, ino, +1);
 
        return 0;
 }
@@ -516,16 +567,24 @@ static errcode_t adjust_inode_count(e2fsck_t ctx, ino_t ino, int adj)
               inode.i_links_count);
 #endif
 
-       inode.i_links_count += adj;
        if (adj == 1) {
                ext2fs_icount_increment(ctx->inode_count, ino, 0);
+               if (inode.i_links_count == (__u16) ~0)
+                       return 0;
                ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
-       } else {
+               inode.i_links_count++;
+       } else if (adj == -1) {
                ext2fs_icount_decrement(ctx->inode_count, ino, 0);
+               if (inode.i_links_count == 0)
+                       return 0;
                ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
+               inode.i_links_count--;
+       } else {
+               /* Should never happen */
+               fatal_error(ctx, _("Debug error in e2fsck adjust_inode_count, "
+                                  "should never happen.\n"));
        }
        
-
        retval = ext2fs_write_inode(fs, ino, &inode);
        if (retval)
                return retval;
@@ -613,9 +672,10 @@ static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ino_t parent)
  */
 
 struct expand_dir_struct {
-       int     done;
-       errcode_t       err;
-       e2fsck_t        ctx;
+       int                     done;
+       int                     newblocks;
+       errcode_t               err;
+       e2fsck_t                ctx;
 };
 
 static int expand_dir_proc(ext2_filsys fs,
@@ -668,6 +728,8 @@ static int expand_dir_proc(ext2_filsys fs,
        ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk);
        ext2fs_mark_block_bitmap(fs->block_map, new_blk);
        ext2fs_mark_bb_dirty(fs);
+       es->newblocks++;
+       
        if (es->done)
                return (BLOCK_CHANGED | BLOCK_ABORT);
        else
@@ -689,13 +751,14 @@ static errcode_t expand_directory(e2fsck_t ctx, ino_t dir)
         * them.
         */
        e2fsck_read_bitmaps(ctx);
-       
+
        retval = ext2fs_check_directory(fs, dir);
        if (retval)
                return retval;
        
        es.done = 0;
        es.err = 0;
+       es.newblocks = 0;
        es.ctx = ctx;
        
        retval = ext2fs_block_iterate(fs, dir, BLOCK_FLAG_APPEND,
@@ -714,7 +777,7 @@ static errcode_t expand_directory(e2fsck_t ctx, ino_t dir)
                return retval;
        
        inode.i_size += fs->blocksize;
-       inode.i_blocks += fs->blocksize / 512;
+       inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
 
        e2fsck_write_inode(ctx, dir, &inode, "expand_directory");