Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * pass2.c --- check directory structure |
| 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% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | * |
| 11 | * Pass 2 of e2fsck iterates through all active directory inodes, and |
| 12 | * applies to following tests to each directory entry in the directory |
| 13 | * blocks in the inodes: |
| 14 | * |
| 15 | * - The length of the directory entry (rec_len) should be at |
| 16 | * least 8 bytes, and no more than the remaining space |
| 17 | * left in the directory block. |
| 18 | * - The length of the name in the directory entry (name_len) |
| 19 | * should be less than (rec_len - 8). |
| 20 | * - The inode number in the directory entry should be within |
| 21 | * legal bounds. |
| 22 | * - The inode number should refer to a in-use inode. |
| 23 | * - The first entry should be '.', and its inode should be |
| 24 | * the inode of the directory. |
| 25 | * - The second entry should be '..'. |
| 26 | * |
| 27 | * To minimize disk seek time, the directory blocks are processed in |
| 28 | * sorted order of block numbers. |
| 29 | * |
| 30 | * Pass 2 also collects the following information: |
| 31 | * - The inode numbers of the subdirectories for each directory. |
| 32 | * |
| 33 | * Pass 2 relies on the following information from previous passes: |
| 34 | * - The directory information collected in pass 1. |
| 35 | * - The inode_used_map bitmap |
| 36 | * - The inode_bad_map bitmap |
| 37 | * - The inode_dir_map bitmap |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 38 | * |
| 39 | * Pass 2 frees the following data structures |
| 40 | * - The inode_bad_map bitmap |
| 41 | */ |
| 42 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 43 | #include "e2fsck.h" |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 44 | #include "problem.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Keeps track of how many times an inode is referenced. |
| 48 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 49 | static void deallocate_inode(e2fsck_t ctx, ino_t ino, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 50 | char* block_buf); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 51 | static int check_dir_block(ext2_filsys fs, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 52 | struct ext2_db_entry *dir_blocks_info, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 53 | void *priv_data); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 54 | static int allocate_dir_block(e2fsck_t ctx, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 55 | struct ext2_db_entry *dir_blocks_info, |
| 56 | char *buf, struct problem_context *pctx); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 57 | static int update_dir_block(ext2_filsys fs, |
| 58 | blk_t *block_nr, |
| 59 | int blockcnt, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 60 | void *priv_data); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 61 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 62 | struct check_dir_struct { |
| 63 | char *buf; |
| 64 | struct problem_context pctx; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 65 | int count, max; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 66 | e2fsck_t ctx; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 69 | void e2fsck_pass2(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 70 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 71 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 72 | char *buf; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 73 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 74 | struct resource_track rtrack; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 75 | #endif |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 76 | struct dir_info *dir; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 77 | struct check_dir_struct cd; |
| 78 | |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 79 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 80 | init_resource_track(&rtrack); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 81 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 82 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 83 | clear_problem_context(&cd.pctx); |
| 84 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 85 | #ifdef MTRACE |
| 86 | mtrace_print("Pass 2"); |
| 87 | #endif |
| 88 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 89 | if (!(ctx->options & E2F_OPT_PREEN)) |
| 90 | fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx); |
| 91 | |
| 92 | cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT, |
| 93 | 0, ctx->inode_link_info, |
| 94 | &ctx->inode_count); |
| 95 | if (cd.pctx.errcode) { |
| 96 | fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 97 | ctx->flags |= E2F_FLAG_ABORT; |
| 98 | return; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 99 | } |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 100 | buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize, |
| 101 | "directory scan buffer"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 102 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 103 | /* |
| 104 | * Set up the parent pointer for the root directory, if |
| 105 | * present. (If the root directory is not present, we will |
| 106 | * create it in pass 3.) |
| 107 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 108 | dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 109 | if (dir) |
| 110 | dir->parent = EXT2_ROOT_INO; |
| 111 | |
| 112 | cd.buf = buf; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 113 | cd.ctx = ctx; |
Theodore Ts'o | f75c28d | 1998-08-01 04:18:06 +0000 | [diff] [blame] | 114 | cd.count = 1; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 115 | cd.max = ext2fs_dblist_count(fs->dblist); |
Theodore Ts'o | f75c28d | 1998-08-01 04:18:06 +0000 | [diff] [blame] | 116 | |
| 117 | if (ctx->progress) |
| 118 | (void) (ctx->progress)(ctx, 2, 0, cd.max); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 119 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 120 | cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block, |
| 121 | &cd); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 122 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 123 | return; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 124 | if (cd.pctx.errcode) { |
| 125 | fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 126 | ctx->flags |= E2F_FLAG_ABORT; |
| 127 | return; |
Theodore Ts'o | 7ac02a5 | 1997-06-11 18:32:35 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 130 | ext2fs_free_mem((void **) &buf); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 131 | ext2fs_free_dblist(fs->dblist); |
| 132 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 133 | if (ctx->inode_bad_map) { |
| 134 | ext2fs_free_inode_bitmap(ctx->inode_bad_map); |
| 135 | ctx->inode_bad_map = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 136 | } |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 137 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 138 | if (ctx->options & E2F_OPT_TIME2) |
| 139 | print_resource_track("Pass 2", &rtrack); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 140 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Make sure the first entry in the directory is '.', and that the |
| 145 | * directory entry is sane. |
| 146 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 147 | static int check_dot(e2fsck_t ctx, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 148 | struct ext2_dir_entry *dirent, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 149 | ino_t ino, struct problem_context *pctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 150 | { |
| 151 | struct ext2_dir_entry *nextdir; |
| 152 | int status = 0; |
| 153 | int created = 0; |
| 154 | int new_len; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 155 | int problem = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 156 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 157 | if (!dirent->inode) |
| 158 | problem = PR_2_MISSING_DOT; |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 159 | else if (((dirent->name_len & 0xFF) != 1) || |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 160 | (dirent->name[0] != '.')) |
| 161 | problem = PR_2_1ST_NOT_DOT; |
| 162 | else if (dirent->name[1] != '\0') |
| 163 | problem = PR_2_DOT_NULL_TERM; |
| 164 | |
| 165 | if (problem) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 166 | if (fix_problem(ctx, problem, pctx)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 167 | if (dirent->rec_len < 12) |
| 168 | dirent->rec_len = 12; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 169 | dirent->inode = ino; |
| 170 | dirent->name_len = 1; |
| 171 | dirent->name[0] = '.'; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 172 | dirent->name[1] = '\0'; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 173 | status = 1; |
| 174 | created = 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 177 | if (dirent->inode != ino) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 178 | if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 179 | dirent->inode = ino; |
| 180 | status = 1; |
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 | if (dirent->rec_len > 12) { |
| 184 | new_len = dirent->rec_len - 12; |
| 185 | if (new_len > 12) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 186 | if (created || |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 187 | fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 188 | nextdir = (struct ext2_dir_entry *) |
| 189 | ((char *) dirent + 12); |
| 190 | dirent->rec_len = 12; |
| 191 | nextdir->rec_len = new_len; |
| 192 | nextdir->inode = 0; |
| 193 | nextdir->name_len = 0; |
| 194 | status = 1; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | return status; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Make sure the second entry in the directory is '..', and that the |
| 203 | * directory entry is sane. We do not check the inode number of '..' |
| 204 | * here; this gets done in pass 3. |
| 205 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 206 | static int check_dotdot(e2fsck_t ctx, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 207 | struct ext2_dir_entry *dirent, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 208 | struct dir_info *dir, struct problem_context *pctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 209 | { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 210 | int problem = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 211 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 212 | if (!dirent->inode) |
| 213 | problem = PR_2_MISSING_DOT_DOT; |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 214 | else if (((dirent->name_len & 0xFF) != 2) || |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 215 | (dirent->name[0] != '.') || |
| 216 | (dirent->name[1] != '.')) |
| 217 | problem = PR_2_2ND_NOT_DOT_DOT; |
| 218 | else if (dirent->name[2] != '\0') |
| 219 | problem = PR_2_DOT_DOT_NULL_TERM; |
| 220 | |
| 221 | if (problem) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 222 | if (fix_problem(ctx, problem, pctx)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 223 | if (dirent->rec_len < 12) |
| 224 | dirent->rec_len = 12; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 225 | /* |
| 226 | * Note: we don't have the parent inode just |
| 227 | * yet, so we will fill it in with the root |
| 228 | * inode. This will get fixed in pass 3. |
| 229 | */ |
| 230 | dirent->inode = EXT2_ROOT_INO; |
| 231 | dirent->name_len = 2; |
| 232 | dirent->name[0] = '.'; |
| 233 | dirent->name[1] = '.'; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 234 | dirent->name[2] = '\0'; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 235 | return 1; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 236 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 237 | return 0; |
| 238 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 239 | dir->dotdot = dirent->inode; |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Check to make sure a directory entry doesn't contain any illegal |
| 245 | * characters. |
| 246 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 247 | static int check_name(e2fsck_t ctx, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 248 | struct ext2_dir_entry *dirent, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 249 | ino_t dir_ino, struct problem_context *pctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 250 | { |
| 251 | int i; |
| 252 | int fixup = -1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 253 | int ret = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 254 | |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 255 | for ( i = 0; i < (dirent->name_len & 0xFF); i++) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 256 | if (dirent->name[i] == '/' || dirent->name[i] == '\0') { |
| 257 | if (fixup < 0) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 258 | fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 259 | } |
| 260 | if (fixup) { |
| 261 | dirent->name[i] = '.'; |
| 262 | ret = 1; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 263 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static int check_dir_block(ext2_filsys fs, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 270 | struct ext2_db_entry *db, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 271 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 272 | { |
| 273 | struct dir_info *subdir, *dir; |
| 274 | struct ext2_dir_entry *dirent; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 275 | int offset = 0; |
| 276 | int dir_modified = 0; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 277 | int dot_state; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 278 | blk_t block_nr = db->blk; |
| 279 | ino_t ino = db->ino; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 280 | __u16 links; |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 281 | struct check_dir_struct *cd; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 282 | char *buf; |
| 283 | e2fsck_t ctx; |
| 284 | int problem; |
| 285 | |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 286 | cd = (struct check_dir_struct *) priv_data; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 287 | buf = cd->buf; |
| 288 | ctx = cd->ctx; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 289 | |
| 290 | if (ctx->progress) |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 291 | if ((ctx->progress)(ctx, 2, cd->count++, cd->max)) |
| 292 | return DIRENT_ABORT; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 293 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 294 | /* |
| 295 | * Make sure the inode is still in use (could have been |
| 296 | * deleted in the duplicate/bad blocks pass. |
| 297 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 298 | if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino))) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 299 | return 0; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 300 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 301 | cd->pctx.ino = ino; |
| 302 | cd->pctx.blk = block_nr; |
| 303 | cd->pctx.blkcount = db->blockcnt; |
| 304 | cd->pctx.ino2 = 0; |
| 305 | cd->pctx.dirent = 0; |
| 306 | cd->pctx.num = 0; |
| 307 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 308 | if (db->blk == 0) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 309 | if (allocate_dir_block(ctx, db, buf, &cd->pctx)) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 310 | return 0; |
| 311 | block_nr = db->blk; |
| 312 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 313 | |
| 314 | if (db->blockcnt) |
| 315 | dot_state = 2; |
| 316 | else |
| 317 | dot_state = 0; |
| 318 | |
| 319 | #if 0 |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 320 | printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 321 | db->blockcnt, ino); |
| 322 | #endif |
| 323 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 324 | cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf); |
| 325 | if (cd->pctx.errcode) { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 326 | if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) { |
| 327 | ctx->flags |= E2F_FLAG_ABORT; |
| 328 | return DIRENT_ABORT; |
| 329 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 330 | memset(buf, 0, fs->blocksize); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | do { |
| 334 | dot_state++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 335 | problem = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 336 | dirent = (struct ext2_dir_entry *) (buf + offset); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 337 | cd->pctx.dirent = dirent; |
| 338 | cd->pctx.num = offset; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 339 | if (((offset + dirent->rec_len) > fs->blocksize) || |
| 340 | (dirent->rec_len < 8) || |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 341 | ((dirent->rec_len % 4) != 0) || |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 342 | (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 343 | if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 344 | dirent->rec_len = fs->blocksize - offset; |
| 345 | dirent->name_len = 0; |
| 346 | dirent->inode = 0; |
| 347 | dir_modified++; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 348 | } else |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 349 | return DIRENT_ABORT; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 350 | } |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 351 | if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 352 | if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 353 | dirent->name_len = EXT2_NAME_LEN; |
| 354 | dir_modified++; |
| 355 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 358 | if (dot_state == 1) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 359 | if (check_dot(ctx, dirent, ino, &cd->pctx)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 360 | dir_modified++; |
| 361 | } else if (dot_state == 2) { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 362 | dir = e2fsck_get_dir_info(ctx, ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 363 | if (!dir) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 364 | fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 365 | ctx->flags |= E2F_FLAG_ABORT; |
| 366 | return DIRENT_ABORT; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 367 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 368 | if (check_dotdot(ctx, dirent, dir, &cd->pctx)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 369 | dir_modified++; |
| 370 | } else if (dirent->inode == ino) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 371 | problem = PR_2_LINK_DOT; |
| 372 | if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 373 | dirent->inode = 0; |
| 374 | dir_modified++; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 375 | goto next; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | if (!dirent->inode) |
| 379 | goto next; |
| 380 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 381 | /* |
| 382 | * Make sure the inode listed is a legal one. |
| 383 | */ |
| 384 | if (((dirent->inode != EXT2_ROOT_INO) && |
Theodore Ts'o | 7f88b04 | 1997-04-26 14:48:50 +0000 | [diff] [blame] | 385 | (dirent->inode < EXT2_FIRST_INODE(fs->super))) || |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 386 | (dirent->inode > fs->super->s_inodes_count)) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 387 | problem = PR_2_BAD_INO; |
| 388 | } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 389 | dirent->inode))) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 390 | /* |
| 391 | * If the inode is unused, offer to clear it. |
| 392 | */ |
| 393 | problem = PR_2_UNUSED_INODE; |
| 394 | } else if (ctx->inode_bb_map && |
| 395 | (ext2fs_test_inode_bitmap(ctx->inode_bb_map, |
| 396 | dirent->inode))) { |
| 397 | /* |
| 398 | * If the inode is in a bad block, offer to |
| 399 | * clear it. |
| 400 | */ |
| 401 | problem = PR_2_BB_INODE; |
| 402 | } else if ((dot_state > 2) && |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 403 | ((dirent->name_len & 0xFF) == 1) && |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 404 | (dirent->name[0] == '.')) { |
| 405 | /* |
| 406 | * If there's a '.' entry in anything other |
| 407 | * than the first directory entry, it's a |
| 408 | * duplicate entry that should be removed. |
| 409 | */ |
| 410 | problem = PR_2_DUP_DOT; |
| 411 | } else if ((dot_state > 2) && |
Theodore Ts'o | b6f7983 | 1998-03-09 13:10:37 +0000 | [diff] [blame] | 412 | ((dirent->name_len & 0xFF) == 2) && |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 413 | (dirent->name[0] == '.') && |
| 414 | (dirent->name[1] == '.')) { |
| 415 | /* |
| 416 | * If there's a '..' entry in anything other |
| 417 | * than the second directory entry, it's a |
| 418 | * duplicate entry that should be removed. |
| 419 | */ |
| 420 | problem = PR_2_DUP_DOT_DOT; |
| 421 | } else if ((dot_state > 2) && |
| 422 | (dirent->inode == EXT2_ROOT_INO)) { |
| 423 | /* |
| 424 | * Don't allow links to the root directory. |
| 425 | * We check this specially to make sure we |
| 426 | * catch this error case even if the root |
| 427 | * directory hasn't been created yet. |
| 428 | */ |
| 429 | problem = PR_2_LINK_ROOT; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 432 | if (problem) { |
| 433 | if (fix_problem(ctx, problem, &cd->pctx)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 434 | dirent->inode = 0; |
| 435 | dir_modified++; |
| 436 | goto next; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 437 | } else { |
| 438 | ext2fs_unmark_valid(fs); |
| 439 | if (problem == PR_2_BAD_INO) |
| 440 | goto next; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 441 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* |
| 445 | * If the inode was marked as having bad fields in |
| 446 | * pass1, process it and offer to fix/clear it. |
| 447 | * (We wait until now so that we can display the |
| 448 | * pathname to the user.) |
| 449 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 450 | if (ctx->inode_bad_map && |
| 451 | ext2fs_test_inode_bitmap(ctx->inode_bad_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 452 | dirent->inode)) { |
Theodore Ts'o | e72a9ba | 1999-06-25 15:40:18 +0000 | [diff] [blame] | 453 | if (e2fsck_process_bad_inode(ctx, ino, |
| 454 | dirent->inode)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 455 | dirent->inode = 0; |
| 456 | dir_modified++; |
| 457 | goto next; |
| 458 | } |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 459 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 460 | return DIRENT_ABORT; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 463 | if (check_name(ctx, dirent, ino, &cd->pctx)) |
| 464 | dir_modified++; |
| 465 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 466 | /* |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 467 | * If this is a directory, then mark its parent in its |
| 468 | * dir_info structure. If the parent field is already |
| 469 | * filled in, then this directory has more than one |
| 470 | * hard link. We assume the first link is correct, |
| 471 | * and ask the user if he/she wants to clear this one. |
| 472 | */ |
| 473 | if ((dot_state > 2) && |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 474 | (ext2fs_test_inode_bitmap(ctx->inode_dir_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 475 | dirent->inode))) { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 476 | subdir = e2fsck_get_dir_info(ctx, dirent->inode); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 477 | if (!subdir) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 478 | cd->pctx.ino = dirent->inode; |
| 479 | fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 480 | ctx->flags |= E2F_FLAG_ABORT; |
| 481 | return DIRENT_ABORT; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 482 | } |
| 483 | if (subdir->parent) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 484 | cd->pctx.ino2 = subdir->parent; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 485 | if (fix_problem(ctx, PR_2_LINK_DIR, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 486 | &cd->pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 487 | dirent->inode = 0; |
| 488 | dir_modified++; |
| 489 | goto next; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 490 | } |
| 491 | cd->pctx.ino2 = 0; |
| 492 | } else |
| 493 | subdir->parent = ino; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 496 | ext2fs_icount_increment(ctx->inode_count, dirent->inode, |
| 497 | &links); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 498 | if (links > 1) |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 499 | ctx->fs_links_count++; |
| 500 | ctx->fs_total_count++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 501 | next: |
| 502 | offset += dirent->rec_len; |
| 503 | } while (offset < fs->blocksize); |
| 504 | #if 0 |
| 505 | printf("\n"); |
| 506 | #endif |
| 507 | if (offset != fs->blocksize) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 508 | cd->pctx.num = dirent->rec_len - fs->blocksize + offset; |
| 509 | if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) { |
| 510 | dirent->rec_len = cd->pctx.num; |
| 511 | dir_modified++; |
| 512 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 513 | } |
| 514 | if (dir_modified) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 515 | cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf); |
| 516 | if (cd->pctx.errcode) { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 517 | if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK, |
| 518 | &cd->pctx)) { |
| 519 | ctx->flags |= E2F_FLAG_ABORT; |
| 520 | return DIRENT_ABORT; |
| 521 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 522 | } |
| 523 | ext2fs_mark_changed(fs); |
| 524 | } |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * This function is called to deallocate a block, and is an interator |
| 530 | * functioned called by deallocate inode via ext2fs_iterate_block(). |
| 531 | */ |
| 532 | static int deallocate_inode_block(ext2_filsys fs, |
| 533 | blk_t *block_nr, |
| 534 | int blockcnt, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 535 | void *priv_data) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 536 | { |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 537 | e2fsck_t ctx = (e2fsck_t) priv_data; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 538 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 539 | if (!*block_nr) |
| 540 | return 0; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 541 | ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 542 | ext2fs_unmark_block_bitmap(fs->block_map, *block_nr); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | * This fuction deallocates an inode |
| 548 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 549 | static void deallocate_inode(e2fsck_t ctx, ino_t ino, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 550 | char* block_buf) |
| 551 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 552 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 553 | struct ext2_inode inode; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 554 | struct problem_context pctx; |
| 555 | |
| 556 | ext2fs_icount_store(ctx->inode_link_info, ino, 0); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 557 | e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 558 | inode.i_links_count = 0; |
| 559 | inode.i_dtime = time(0); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 560 | e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode"); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 561 | clear_problem_context(&pctx); |
| 562 | pctx.ino = ino; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 563 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 564 | /* |
| 565 | * Fix up the bitmaps... |
| 566 | */ |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 567 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 568 | ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino); |
| 569 | ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino); |
| 570 | if (ctx->inode_bad_map) |
| 571 | ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 572 | ext2fs_unmark_inode_bitmap(fs->inode_map, ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 573 | ext2fs_mark_ib_dirty(fs); |
| 574 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 575 | if (!ext2fs_inode_has_valid_blocks(&inode)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 576 | return; |
| 577 | |
| 578 | ext2fs_mark_bb_dirty(fs); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 579 | pctx.errcode = ext2fs_block_iterate(fs, ino, 0, block_buf, |
| 580 | deallocate_inode_block, ctx); |
| 581 | if (pctx.errcode) { |
| 582 | fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 583 | ctx->flags |= E2F_FLAG_ABORT; |
| 584 | return; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 585 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Theodore Ts'o | e72a9ba | 1999-06-25 15:40:18 +0000 | [diff] [blame] | 588 | extern int e2fsck_process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 589 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 590 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 591 | struct ext2_inode inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 592 | int inode_modified = 0; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 593 | unsigned char *frag, *fsize; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 594 | struct problem_context pctx; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 595 | int problem = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 596 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 597 | e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode"); |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 598 | |
| 599 | clear_problem_context(&pctx); |
| 600 | pctx.ino = ino; |
| 601 | pctx.dir = dir; |
| 602 | pctx.inode = &inode; |
| 603 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 604 | if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) && |
| 605 | !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) && |
| 606 | !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) && |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 607 | !(LINUX_S_ISSOCK(inode.i_mode))) |
| 608 | problem = PR_2_BAD_MODE; |
Theodore Ts'o | 7cf73dc | 1997-08-14 17:17:16 +0000 | [diff] [blame] | 609 | |
| 610 | if (LINUX_S_ISCHR(inode.i_mode) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 611 | && !e2fsck_pass1_check_device_inode(&inode)) |
| 612 | problem = PR_2_BAD_CHAR_DEV; |
Theodore Ts'o | 7cf73dc | 1997-08-14 17:17:16 +0000 | [diff] [blame] | 613 | |
| 614 | if (LINUX_S_ISBLK(inode.i_mode) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 615 | && !e2fsck_pass1_check_device_inode(&inode)) |
| 616 | problem = PR_2_BAD_BLOCK_DEV; |
| 617 | |
Theodore Ts'o | 1dde43f | 1998-11-14 04:18:28 +0000 | [diff] [blame] | 618 | if (LINUX_S_ISFIFO(inode.i_mode) |
| 619 | && !e2fsck_pass1_check_device_inode(&inode)) |
| 620 | problem = PR_2_BAD_FIFO; |
| 621 | |
| 622 | if (LINUX_S_ISSOCK(inode.i_mode) |
| 623 | && !e2fsck_pass1_check_device_inode(&inode)) |
| 624 | problem = PR_2_BAD_SOCKET; |
| 625 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 626 | if (problem) { |
| 627 | if (fix_problem(ctx, problem, &pctx)) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 628 | deallocate_inode(ctx, ino, 0); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 629 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 630 | return 0; |
Theodore Ts'o | 7cf73dc | 1997-08-14 17:17:16 +0000 | [diff] [blame] | 631 | return 1; |
| 632 | } |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 633 | problem = 0; |
Theodore Ts'o | 7cf73dc | 1997-08-14 17:17:16 +0000 | [diff] [blame] | 634 | } |
Theodore Ts'o | 7cf73dc | 1997-08-14 17:17:16 +0000 | [diff] [blame] | 635 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 636 | if (inode.i_faddr && |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 637 | fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 638 | inode.i_faddr = 0; |
| 639 | inode_modified++; |
| 640 | } |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 641 | |
| 642 | switch (fs->super->s_creator_os) { |
| 643 | case EXT2_OS_LINUX: |
| 644 | frag = &inode.osd2.linux2.l_i_frag; |
| 645 | fsize = &inode.osd2.linux2.l_i_fsize; |
| 646 | break; |
| 647 | case EXT2_OS_HURD: |
| 648 | frag = &inode.osd2.hurd2.h_i_frag; |
| 649 | fsize = &inode.osd2.hurd2.h_i_fsize; |
| 650 | break; |
| 651 | case EXT2_OS_MASIX: |
| 652 | frag = &inode.osd2.masix2.m_i_frag; |
| 653 | fsize = &inode.osd2.masix2.m_i_fsize; |
| 654 | break; |
| 655 | default: |
| 656 | frag = fsize = 0; |
| 657 | } |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 658 | if (frag && *frag) { |
| 659 | pctx.num = *frag; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 660 | if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 661 | *frag = 0; |
| 662 | inode_modified++; |
| 663 | } |
| 664 | pctx.num = 0; |
| 665 | } |
| 666 | if (fsize && *fsize) { |
| 667 | pctx.num = *fsize; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 668 | if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 669 | *fsize = 0; |
| 670 | inode_modified++; |
| 671 | } |
| 672 | pctx.num = 0; |
| 673 | } |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 674 | |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 675 | if (inode.i_file_acl && |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 676 | fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 677 | inode.i_file_acl = 0; |
| 678 | inode_modified++; |
| 679 | } |
| 680 | if (inode.i_dir_acl && |
Theodore Ts'o | 246501c | 1998-03-24 16:22:38 +0000 | [diff] [blame] | 681 | LINUX_S_ISDIR(inode.i_mode) && |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 682 | fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) { |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 683 | inode.i_dir_acl = 0; |
| 684 | inode_modified++; |
| 685 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 686 | if (inode_modified) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 687 | e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 688 | return 0; |
| 689 | } |
| 690 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 691 | |
| 692 | /* |
| 693 | * allocate_dir_block --- this function allocates a new directory |
| 694 | * block for a particular inode; this is done if a directory has |
| 695 | * a "hole" in it, or if a directory has a illegal block number |
| 696 | * that was zeroed out and now needs to be replaced. |
| 697 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 698 | static int allocate_dir_block(e2fsck_t ctx, |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 699 | struct ext2_db_entry *db, |
| 700 | char *buf, struct problem_context *pctx) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 701 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 702 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 703 | blk_t blk; |
| 704 | char *block; |
| 705 | struct ext2_inode inode; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 706 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 707 | if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 708 | return 1; |
| 709 | |
| 710 | /* |
| 711 | * Read the inode and block bitmaps in; we'll be messing with |
| 712 | * them. |
| 713 | */ |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 714 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 715 | |
| 716 | /* |
| 717 | * First, find a free block |
| 718 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 719 | pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk); |
| 720 | if (pctx->errcode) { |
| 721 | pctx->str = "ext2fs_new_block"; |
| 722 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 723 | return 1; |
| 724 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 725 | ext2fs_mark_block_bitmap(ctx->block_found_map, blk); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 726 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
| 727 | ext2fs_mark_bb_dirty(fs); |
| 728 | |
| 729 | /* |
| 730 | * Now let's create the actual data block for the inode |
| 731 | */ |
| 732 | if (db->blockcnt) |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 733 | pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 734 | else |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 735 | pctx->errcode = ext2fs_new_dir_block(fs, db->ino, |
| 736 | EXT2_ROOT_INO, &block); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 737 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 738 | if (pctx->errcode) { |
| 739 | pctx->str = "ext2fs_new_dir_block"; |
| 740 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 741 | return 1; |
| 742 | } |
| 743 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 744 | pctx->errcode = ext2fs_write_dir_block(fs, blk, block); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 745 | ext2fs_free_mem((void **) &block); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 746 | if (pctx->errcode) { |
| 747 | pctx->str = "ext2fs_write_dir_block"; |
| 748 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 749 | return 1; |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * Update the inode block count |
| 754 | */ |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 755 | e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block"); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 756 | inode.i_blocks += fs->blocksize / 512; |
| 757 | if (inode.i_size < (db->blockcnt+1) * fs->blocksize) |
| 758 | inode.i_size = (db->blockcnt+1) * fs->blocksize; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 759 | e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block"); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 760 | |
| 761 | /* |
| 762 | * Finally, update the block pointers for the inode |
| 763 | */ |
| 764 | db->blk = blk; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 765 | pctx->errcode = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE, |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 766 | 0, update_dir_block, db); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 767 | if (pctx->errcode) { |
| 768 | pctx->str = "ext2fs_block_iterate"; |
| 769 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 770 | return 1; |
| 771 | } |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * This is a helper function for allocate_dir_block(). |
| 778 | */ |
| 779 | static int update_dir_block(ext2_filsys fs, |
| 780 | blk_t *block_nr, |
| 781 | int blockcnt, |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 782 | void *priv_data) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 783 | { |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 784 | struct ext2_db_entry *db; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 785 | |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 786 | db = (struct ext2_db_entry *) priv_data; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 787 | if (db->blockcnt == blockcnt) { |
| 788 | *block_nr = db->blk; |
| 789 | return BLOCK_CHANGED; |
| 790 | } |
| 791 | return 0; |
| 792 | } |