Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * pass4.c -- pass #4 of e2fsck: Check reference counts |
| 3 | * |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 4 | * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
| 10 | * |
| 11 | * Pass 4 frees the following data structures: |
| 12 | * - A bitmap of which inodes are in bad blocks. (inode_bb_map) |
Theodore Ts'o | aa4115a | 1999-10-21 19:33:18 +0000 | [diff] [blame] | 13 | * - A bitmap of which inodes are imagic inodes. (inode_imagic_map) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "e2fsck.h" |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 17 | #include "problem.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 18 | |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 19 | /* |
| 20 | * This routine is called when an inode is not connected to the |
| 21 | * directory tree. |
| 22 | * |
| 23 | * This subroutine returns 1 then the caller shouldn't bother with the |
| 24 | * rest of the pass 4 tests. |
| 25 | */ |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 26 | static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i) |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 27 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 28 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 29 | struct ext2_inode inode; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 30 | struct problem_context pctx; |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 31 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 32 | e2fsck_read_inode(ctx, i, &inode, "pass4: disconnect_inode"); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 33 | clear_problem_context(&pctx); |
| 34 | pctx.ino = i; |
| 35 | pctx.inode = &inode; |
| 36 | |
Theodore Ts'o | 0684a4f | 2002-08-17 10:19:44 -0400 | [diff] [blame] | 37 | /* |
| 38 | * Offer to delete any zero-length files that does not have |
| 39 | * blocks. If there is an EA block, it might have useful |
| 40 | * information, so we won't prompt to delete it, but let it be |
| 41 | * reconnected to lost+found. |
| 42 | */ |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 43 | if (!inode.i_blocks && (LINUX_S_ISREG(inode.i_mode) || |
| 44 | LINUX_S_ISDIR(inode.i_mode))) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 45 | if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) { |
| 46 | ext2fs_icount_store(ctx->inode_link_info, i, 0); |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 47 | inode.i_links_count = 0; |
| 48 | inode.i_dtime = time(0); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 49 | e2fsck_write_inode(ctx, i, &inode, |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 50 | "disconnect_inode"); |
| 51 | /* |
| 52 | * Fix up the bitmaps... |
| 53 | */ |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 54 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 55 | ext2fs_unmark_inode_bitmap(ctx->inode_used_map, i); |
| 56 | ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, i); |
Theodore Ts'o | 0684a4f | 2002-08-17 10:19:44 -0400 | [diff] [blame] | 57 | ext2fs_inode_alloc_stats2(fs, i, -1, |
| 58 | LINUX_S_ISDIR(inode.i_mode)); |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Prompt to reconnect. |
| 65 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 66 | if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 67 | if (e2fsck_reconnect_file(ctx, i)) |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 68 | ext2fs_unmark_valid(fs); |
| 69 | } else { |
| 70 | /* |
| 71 | * If we don't attach the inode, then skip the |
| 72 | * i_links_test since there's no point in trying to |
| 73 | * force i_links_count to zero. |
| 74 | */ |
| 75 | ext2fs_unmark_valid(fs); |
| 76 | return 1; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 82 | void e2fsck_pass4(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 83 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 84 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 85 | ext2_ino_t i; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 86 | struct ext2_inode inode; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 87 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 88 | struct resource_track rtrack; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 89 | #endif |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 90 | struct problem_context pctx; |
| 91 | __u16 link_count, link_counted; |
Theodore Ts'o | bcf9c5d | 2002-05-21 09:14:17 -0400 | [diff] [blame] | 92 | char *buf = 0; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 93 | int group, maxgroup; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 94 | |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 95 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 96 | init_resource_track(&rtrack); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 97 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 98 | |
| 99 | #ifdef MTRACE |
| 100 | mtrace_print("Pass 4"); |
| 101 | #endif |
| 102 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 103 | clear_problem_context(&pctx); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 104 | |
| 105 | if (!(ctx->options & E2F_OPT_PREEN)) |
| 106 | fix_problem(ctx, PR_4_PASS_HEADER, &pctx); |
| 107 | |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 108 | group = 0; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 109 | maxgroup = fs->group_desc_count; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 110 | if (ctx->progress) |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 111 | if ((ctx->progress)(ctx, 4, 0, maxgroup)) |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 112 | return; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 113 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 114 | for (i=1; i <= fs->super->s_inodes_count; i++) { |
Theodore Ts'o | 4cae045 | 2002-07-21 14:14:03 -0400 | [diff] [blame] | 115 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
| 116 | return; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 117 | if ((i % fs->super->s_inodes_per_group) == 0) { |
| 118 | group++; |
| 119 | if (ctx->progress) |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 120 | if ((ctx->progress)(ctx, 4, group, maxgroup)) |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 121 | return; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 122 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 123 | if (i == EXT2_BAD_INO || |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 124 | (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super))) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 125 | continue; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 126 | if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, i)) || |
Theodore Ts'o | aa4115a | 1999-10-21 19:33:18 +0000 | [diff] [blame] | 127 | (ctx->inode_imagic_map && |
| 128 | ext2fs_test_inode_bitmap(ctx->inode_imagic_map, i)) || |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 129 | (ctx->inode_bb_map && |
| 130 | ext2fs_test_inode_bitmap(ctx->inode_bb_map, i))) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 131 | continue; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 132 | ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count); |
| 133 | ext2fs_icount_fetch(ctx->inode_count, i, &link_counted); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 134 | if (link_counted == 0) { |
Theodore Ts'o | bcf9c5d | 2002-05-21 09:14:17 -0400 | [diff] [blame] | 135 | if (!buf) |
| 136 | buf = e2fsck_allocate_memory(ctx, |
| 137 | fs->blocksize, "bad_inode buffer"); |
| 138 | if (e2fsck_process_bad_inode(ctx, 0, i, buf)) |
Theodore Ts'o | e72a9ba | 1999-06-25 15:40:18 +0000 | [diff] [blame] | 139 | continue; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 140 | if (disconnect_inode(ctx, i)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 141 | continue; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 142 | ext2fs_icount_fetch(ctx->inode_link_info, i, |
| 143 | &link_count); |
| 144 | ext2fs_icount_fetch(ctx->inode_count, i, |
| 145 | &link_counted); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 146 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 147 | if (link_counted != link_count) { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 148 | e2fsck_read_inode(ctx, i, &inode, "pass4"); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 149 | pctx.ino = i; |
| 150 | pctx.inode = &inode; |
| 151 | if (link_count != inode.i_links_count) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 152 | pctx.num = link_count; |
| 153 | fix_problem(ctx, |
| 154 | PR_4_INCONSISTENT_COUNT, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 155 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 156 | pctx.num = link_counted; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 157 | if (fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 158 | inode.i_links_count = link_counted; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 159 | e2fsck_write_inode(ctx, i, &inode, "pass4"); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 160 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 163 | ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0; |
| 164 | ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0; |
| 165 | ext2fs_free_inode_bitmap(ctx->inode_bb_map); |
| 166 | ctx->inode_bb_map = 0; |
Theodore Ts'o | 7142db0 | 1999-11-08 18:46:54 +0000 | [diff] [blame] | 167 | ext2fs_free_inode_bitmap(ctx->inode_imagic_map); |
| 168 | ctx->inode_imagic_map = 0; |
Theodore Ts'o | bcf9c5d | 2002-05-21 09:14:17 -0400 | [diff] [blame] | 169 | if (buf) |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame^] | 170 | ext2fs_free_mem(&buf); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 171 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 172 | if (ctx->options & E2F_OPT_TIME2) { |
| 173 | e2fsck_clear_progbar(ctx); |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 174 | print_resource_track(_("Pass 4"), &rtrack); |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 175 | } |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 176 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 177 | } |
| 178 | |