Whamcloud - gitweb
ChangeLog, e2fsck.h, pass1.c, pass2.c, pass4.c, swapfs.c:
[tools/e2fsprogs.git] / e2fsck / pass4.c
index dde578b..e059cc3 100644 (file)
 /*
  * pass4.c -- pass #4 of e2fsck: Check reference counts
  *
- * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
- * under the terms of the GNU Public License.
- * 
+ * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
+ *
+ * %Begin-Header%
+ * This file may be redistributed under the terms of the GNU Public
+ * License.
+ * %End-Header%
+ *
+ * Pass 4 frees the following data structures:
+ *     - A bitmap of which inodes are in bad blocks.   (inode_bb_map)
  */
 
 #include "e2fsck.h"
+#include "problem.h"
+
+/*
+ * This routine is called when an inode is not connected to the
+ * directory tree.
+ * 
+ * This subroutine returns 1 then the caller shouldn't bother with the
+ * rest of the pass 4 tests.
+ */
+static int disconnect_inode(e2fsck_t ctx, ino_t i)
+{
+       ext2_filsys fs = ctx->fs;
+       struct ext2_inode       inode;
+       struct problem_context  pctx;
+
+       e2fsck_read_inode(ctx, i, &inode, "pass4: disconnect_inode");
+       clear_problem_context(&pctx);
+       pctx.ino = i;
+       pctx.inode = &inode;
+       
+       if (!inode.i_blocks && (LINUX_S_ISREG(inode.i_mode) ||
+                               LINUX_S_ISDIR(inode.i_mode))) {
+               /*
+                * This is a zero-length file; prompt to delete it...
+                */
+               if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
+                       ext2fs_icount_store(ctx->inode_link_info, i, 0);
+                       inode.i_links_count = 0;
+                       inode.i_dtime = time(0);
+                       e2fsck_write_inode(ctx, i, &inode,
+                                          "disconnect_inode");
+                       /*
+                        * Fix up the bitmaps...
+                        */
+                       e2fsck_read_bitmaps(ctx);
+                       ext2fs_unmark_inode_bitmap(ctx->inode_used_map, i);
+                       ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, i);
+                       ext2fs_unmark_inode_bitmap(fs->inode_map, i);
+                       ext2fs_mark_ib_dirty(fs);
+                       return 0;
+               }
+       }
+       
+       /*
+        * Prompt to reconnect.
+        */
+       if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
+               if (e2fsck_reconnect_file(ctx, i))
+                       ext2fs_unmark_valid(fs);
+       } else {
+               /*
+                * If we don't attach the inode, then skip the
+                * i_links_test since there's no point in trying to
+                * force i_links_count to zero.
+                */
+               ext2fs_unmark_valid(fs);
+               return 1;
+       }
+       return 0;
+}
+
 
-void pass4(ext2_filsys fs)
+void e2fsck_pass4(e2fsck_t ctx)
 {
+       ext2_filsys fs = ctx->fs;
        ino_t   i;
        struct ext2_inode       inode;
+#ifdef RESOURCE_TRACK
        struct resource_track   rtrack;
+#endif
+       struct problem_context  pctx;
+       __u16   link_count, link_counted;
+       int     group, maxgroup;
        
+#ifdef RESOURCE_TRACK
        init_resource_track(&rtrack);
+#endif
 
 #ifdef MTRACE
        mtrace_print("Pass 4");
 #endif
 
-       if (!preen)
-               printf("Pass 4: Checking reference counts\n");
+       clear_problem_context(&pctx);
+
+       if (!(ctx->options & E2F_OPT_PREEN))
+               fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
+
+       group = 0;
+       maxgroup = fs->group_desc_count;
+       if (ctx->progress)
+               if ((ctx->progress)(ctx, 4, 0, maxgroup))
+                       return;
+       
        for (i=1; i <= fs->super->s_inodes_count; i++) {
+               if ((i % fs->super->s_inodes_per_group) == 0) {
+                       group++;
+                       if (ctx->progress)
+                               if ((ctx->progress)(ctx, 4, group, maxgroup))
+                                       return;
+               }
                if (i == EXT2_BAD_INO ||
                    (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
                        continue;
-               if (!(ext2fs_test_inode_bitmap(inode_used_map, i)))
+               if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, i)) ||
+                   (ctx->inode_bb_map &&
+                    ext2fs_test_inode_bitmap(ctx->inode_bb_map, i)))
                        continue;
-               if (inode_count[i] == 0) {
-                       /*
-                        * Inode isn't attached to the filesystem;
-                        * prompt to reconnect.
-                        */
-                       printf("Unattached inode %lu\n", i);
-                       preenhalt(fs);
-                       if (ask("Connect to /lost+found", 1)) {
-                               if (reconnect_file(fs, i))
-                                       ext2fs_unmark_valid(fs);
-                       } else
-                               ext2fs_unmark_valid(fs);
+               ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
+               ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
+               if (link_counted == 0) {
+                       if (e2fsck_process_bad_inode(ctx, 0, i))
+                               continue;
+                       if (disconnect_inode(ctx, i))
+                               continue;
+                       ext2fs_icount_fetch(ctx->inode_link_info, i,
+                                           &link_count);
+                       ext2fs_icount_fetch(ctx->inode_count, i,
+                                           &link_counted);
                }
-               if (inode_count[i] != inode_link_info[i]) {
-                       e2fsck_read_inode(fs, i, &inode, "pass4");
-                       if (inode_link_info[i] != inode.i_links_count) {
-                               printf("WARNING: PROGRAMMING BUG IN E2FSCK!\n");
-                               printf("inode_link_info[%ld] is %u, "
-                                      "inode.i_links_count is %d.  "
-                                      "They should be the same!\n",
-                                      i, inode_link_info[i],
-                                      inode.i_links_count);
+               if (link_counted != link_count) {
+                       e2fsck_read_inode(ctx, i, &inode, "pass4");
+                       pctx.ino = i;
+                       pctx.inode = &inode;
+                       if (link_count != inode.i_links_count) {
+                               pctx.num = link_count;
+                               fix_problem(ctx,
+                                           PR_4_INCONSISTENT_COUNT, &pctx);
+                       }
+                       pctx.num = link_counted;
+                       if (fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
+                               inode.i_links_count = link_counted;
+                               e2fsck_write_inode(ctx, i, &inode, "pass4");
                        }
-                       printf("Inode %lu has ref count %d, expecting %d.\n",
-                              i, inode.i_links_count, inode_count[i]);
-                       if (ask("Set i_nlinks to count", 1)) {
-                               inode.i_links_count = inode_count[i];
-                               e2fsck_write_inode(fs, i, &inode, "pass4");
-                       } else
-                               ext2fs_unmark_valid(fs);
                }
        }
-       free(inode_link_info);  inode_link_info = 0;
-       free(inode_count);      inode_count = 0;
-       if (tflag > 1) {
-               printf("Pass 4: ");
-               print_resource_track(&rtrack);
-       }
+       ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
+       ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
+       ext2fs_free_inode_bitmap(ctx->inode_bb_map);
+       ctx->inode_bb_map = 0;
+#ifdef RESOURCE_TRACK
+       if (ctx->options & E2F_OPT_TIME2)
+               print_resource_track("Pass 4", &rtrack);
+#endif
 }