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" |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 18 | #include <ext2fs/ext2_ext_attr.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 19 | |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 20 | /* |
| 21 | * This routine is called when an inode is not connected to the |
| 22 | * directory tree. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 23 | * |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 24 | * This subroutine returns 1 then the caller shouldn't bother with the |
| 25 | * rest of the pass 4 tests. |
| 26 | */ |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 27 | static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i, |
| 28 | struct ext2_inode *inode) |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 29 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 30 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 31 | struct problem_context pctx; |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 32 | __u32 eamagic = 0; |
| 33 | int extra_size = 0; |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 34 | |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 35 | if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE) { |
| 36 | e2fsck_read_inode_full(ctx, i, inode,EXT2_INODE_SIZE(fs->super), |
| 37 | "pass4: disconnect_inode"); |
| 38 | extra_size = ((struct ext2_inode_large *)inode)->i_extra_isize; |
| 39 | } else { |
| 40 | e2fsck_read_inode(ctx, i, inode, "pass4: disconnect_inode"); |
| 41 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 42 | clear_problem_context(&pctx); |
| 43 | pctx.ino = i; |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 44 | pctx.inode = inode; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 45 | |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 46 | if (EXT2_INODE_SIZE(fs->super) -EXT2_GOOD_OLD_INODE_SIZE -extra_size >0) |
| 47 | eamagic = *(__u32 *)(((char *)inode) +EXT2_GOOD_OLD_INODE_SIZE + |
| 48 | extra_size); |
Theodore Ts'o | 0684a4f | 2002-08-17 10:19:44 -0400 | [diff] [blame] | 49 | /* |
| 50 | * Offer to delete any zero-length files that does not have |
| 51 | * blocks. If there is an EA block, it might have useful |
| 52 | * information, so we won't prompt to delete it, but let it be |
| 53 | * reconnected to lost+found. |
| 54 | */ |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 55 | if (!inode->i_blocks && eamagic != EXT2_EXT_ATTR_MAGIC && |
| 56 | (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode))) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 57 | if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 58 | e2fsck_clear_inode(ctx, i, inode, 0, |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 59 | "disconnect_inode"); |
| 60 | /* |
| 61 | * Fix up the bitmaps... |
| 62 | */ |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 63 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | 0684a4f | 2002-08-17 10:19:44 -0400 | [diff] [blame] | 64 | ext2fs_inode_alloc_stats2(fs, i, -1, |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 65 | LINUX_S_ISDIR(inode->i_mode)); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 66 | quota_data_inodes(ctx->qctx, inode, i, -1); |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 70 | |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 71 | /* |
| 72 | * Prompt to reconnect. |
| 73 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 74 | if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 75 | if (e2fsck_reconnect_file(ctx, i)) |
Theodore Ts'o | 5c57647 | 1997-04-29 15:29:49 +0000 | [diff] [blame] | 76 | ext2fs_unmark_valid(fs); |
| 77 | } else { |
| 78 | /* |
| 79 | * If we don't attach the inode, then skip the |
| 80 | * i_links_test since there's no point in trying to |
| 81 | * force i_links_count to zero. |
| 82 | */ |
| 83 | ext2fs_unmark_valid(fs); |
| 84 | return 1; |
| 85 | } |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 90 | void e2fsck_pass4(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 91 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 92 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 93 | ext2_ino_t i; |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 94 | struct ext2_inode *inode; |
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 | struct resource_track rtrack; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 97 | #endif |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 98 | struct problem_context pctx; |
| 99 | __u16 link_count, link_counted; |
Theodore Ts'o | bcf9c5d | 2002-05-21 09:14:17 -0400 | [diff] [blame] | 100 | char *buf = 0; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 101 | dgrp_t group, maxgroup; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 102 | |
Theodore Ts'o | 6d96b00 | 2007-08-03 20:07:09 -0400 | [diff] [blame] | 103 | init_resource_track(&rtrack, ctx->fs->io); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 104 | |
| 105 | #ifdef MTRACE |
| 106 | mtrace_print("Pass 4"); |
| 107 | #endif |
| 108 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 109 | clear_problem_context(&pctx); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 110 | |
| 111 | if (!(ctx->options & E2F_OPT_PREEN)) |
| 112 | fix_problem(ctx, PR_4_PASS_HEADER, &pctx); |
| 113 | |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 114 | group = 0; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 115 | maxgroup = fs->group_desc_count; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 116 | if (ctx->progress) |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 117 | if ((ctx->progress)(ctx, 4, 0, maxgroup)) |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 118 | return; |
Eric Sandeen | 5830d6b | 2006-08-30 02:16:55 -0400 | [diff] [blame] | 119 | |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 120 | inode = e2fsck_allocate_memory(ctx, EXT2_INODE_SIZE(fs->super), |
| 121 | "scratch inode"); |
| 122 | |
Eric Sandeen | 5830d6b | 2006-08-30 02:16:55 -0400 | [diff] [blame] | 123 | /* Protect loop from wrap-around if s_inodes_count maxed */ |
| 124 | for (i=1; i <= fs->super->s_inodes_count && i > 0; i++) { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 125 | int isdir; |
Theodore Ts'o | 992016c | 2010-11-26 19:09:43 -0500 | [diff] [blame] | 126 | |
Theodore Ts'o | 4cae045 | 2002-07-21 14:14:03 -0400 | [diff] [blame] | 127 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
Brian Behlendorf | 06c9c6f | 2007-03-28 12:43:05 -0400 | [diff] [blame] | 128 | goto errout; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 129 | if ((i % fs->super->s_inodes_per_group) == 0) { |
| 130 | group++; |
| 131 | if (ctx->progress) |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 132 | if ((ctx->progress)(ctx, 4, group, maxgroup)) |
Brian Behlendorf | 06c9c6f | 2007-03-28 12:43:05 -0400 | [diff] [blame] | 133 | goto errout; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 134 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 135 | if (i == EXT2_BAD_INO || |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 136 | (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super))) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 137 | continue; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 138 | if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, i)) || |
Theodore Ts'o | aa4115a | 1999-10-21 19:33:18 +0000 | [diff] [blame] | 139 | (ctx->inode_imagic_map && |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 140 | ext2fs_test_inode_bitmap2(ctx->inode_imagic_map, i)) || |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 141 | (ctx->inode_bb_map && |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 142 | ext2fs_test_inode_bitmap2(ctx->inode_bb_map, i))) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 143 | continue; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 144 | ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count); |
| 145 | ext2fs_icount_fetch(ctx->inode_count, i, &link_counted); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 146 | if (link_counted == 0) { |
Theodore Ts'o | bcf9c5d | 2002-05-21 09:14:17 -0400 | [diff] [blame] | 147 | if (!buf) |
| 148 | buf = e2fsck_allocate_memory(ctx, |
| 149 | fs->blocksize, "bad_inode buffer"); |
| 150 | if (e2fsck_process_bad_inode(ctx, 0, i, buf)) |
Theodore Ts'o | e72a9ba | 1999-06-25 15:40:18 +0000 | [diff] [blame] | 151 | continue; |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 152 | if (disconnect_inode(ctx, i, inode)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 153 | continue; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 154 | ext2fs_icount_fetch(ctx->inode_link_info, i, |
| 155 | &link_count); |
| 156 | ext2fs_icount_fetch(ctx->inode_count, i, |
| 157 | &link_counted); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 158 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 159 | isdir = ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i); |
Theodore Ts'o | 992016c | 2010-11-26 19:09:43 -0500 | [diff] [blame] | 160 | if (isdir && (link_counted > EXT2_LINK_MAX)) |
Andreas Dilger | a7c9cb7 | 2008-02-02 01:25:03 -0700 | [diff] [blame] | 161 | link_counted = 1; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 162 | if (link_counted != link_count) { |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 163 | e2fsck_read_inode(ctx, i, inode, "pass4"); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 164 | pctx.ino = i; |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 165 | pctx.inode = inode; |
Theodore Ts'o | 992016c | 2010-11-26 19:09:43 -0500 | [diff] [blame] | 166 | if ((link_count != inode->i_links_count) && !isdir && |
| 167 | (inode->i_links_count <= EXT2_LINK_MAX)) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 168 | pctx.num = link_count; |
| 169 | fix_problem(ctx, |
| 170 | PR_4_INCONSISTENT_COUNT, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 171 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 172 | pctx.num = link_counted; |
Andreas Dilger | a7c9cb7 | 2008-02-02 01:25:03 -0700 | [diff] [blame] | 173 | /* i_link_count was previously exceeded, but no longer |
| 174 | * is, fix this but don't consider it an error */ |
Theodore Ts'o | 992016c | 2010-11-26 19:09:43 -0500 | [diff] [blame] | 175 | if ((isdir && link_counted > 1 && |
Andreas Dilger | a7c9cb7 | 2008-02-02 01:25:03 -0700 | [diff] [blame] | 176 | (inode->i_flags & EXT2_INDEX_FL) && |
| 177 | link_count == 1 && !(ctx->options & E2F_OPT_NO)) || |
Theodore Ts'o | 992016c | 2010-11-26 19:09:43 -0500 | [diff] [blame] | 178 | fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) { |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 179 | inode->i_links_count = link_counted; |
| 180 | e2fsck_write_inode(ctx, i, inode, "pass4"); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 181 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 184 | ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0; |
| 185 | ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0; |
| 186 | ext2fs_free_inode_bitmap(ctx->inode_bb_map); |
| 187 | ctx->inode_bb_map = 0; |
Theodore Ts'o | 7142db0 | 1999-11-08 18:46:54 +0000 | [diff] [blame] | 188 | ext2fs_free_inode_bitmap(ctx->inode_imagic_map); |
| 189 | ctx->inode_imagic_map = 0; |
Brian Behlendorf | 06c9c6f | 2007-03-28 12:43:05 -0400 | [diff] [blame] | 190 | errout: |
Theodore Ts'o | bcf9c5d | 2002-05-21 09:14:17 -0400 | [diff] [blame] | 191 | if (buf) |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 192 | ext2fs_free_mem(&buf); |
Andreas Dilger | fefaef3 | 2008-02-02 01:16:32 -0700 | [diff] [blame] | 193 | |
| 194 | ext2fs_free_mem(&inode); |
Ken Chen | 9facd07 | 2009-05-28 09:55:10 -0400 | [diff] [blame] | 195 | print_resource_track(ctx, _("Pass 4"), &rtrack, ctx->fs->io); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 196 | } |
| 197 | |