Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * pass2.c --- check directory structure |
| 3 | * |
| 4 | * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be |
| 5 | * redistributed under the terms of the GNU Public License. |
| 6 | * |
| 7 | * Pass 2 of e2fsck iterates through all active directory inodes, and |
| 8 | * applies to following tests to each directory entry in the directory |
| 9 | * blocks in the inodes: |
| 10 | * |
| 11 | * - The length of the directory entry (rec_len) should be at |
| 12 | * least 8 bytes, and no more than the remaining space |
| 13 | * left in the directory block. |
| 14 | * - The length of the name in the directory entry (name_len) |
| 15 | * should be less than (rec_len - 8). |
| 16 | * - The inode number in the directory entry should be within |
| 17 | * legal bounds. |
| 18 | * - The inode number should refer to a in-use inode. |
| 19 | * - The first entry should be '.', and its inode should be |
| 20 | * the inode of the directory. |
| 21 | * - The second entry should be '..'. |
| 22 | * |
| 23 | * To minimize disk seek time, the directory blocks are processed in |
| 24 | * sorted order of block numbers. |
| 25 | * |
| 26 | * Pass 2 also collects the following information: |
| 27 | * - The inode numbers of the subdirectories for each directory. |
| 28 | * |
| 29 | * Pass 2 relies on the following information from previous passes: |
| 30 | * - The directory information collected in pass 1. |
| 31 | * - The inode_used_map bitmap |
| 32 | * - The inode_bad_map bitmap |
| 33 | * - The inode_dir_map bitmap |
| 34 | * - The block_dup_map bitmap |
| 35 | * |
| 36 | * Pass 2 frees the following data structures |
| 37 | * - The inode_bad_map bitmap |
| 38 | */ |
| 39 | |
| 40 | #include "et/com_err.h" |
| 41 | |
| 42 | #include "e2fsck.h" |
| 43 | |
| 44 | /* |
| 45 | * Keeps track of how many times an inode is referenced. |
| 46 | */ |
| 47 | unsigned short * inode_count; |
| 48 | |
| 49 | static void deallocate_inode(ext2_filsys fs, ino_t ino, |
| 50 | char* block_buf); |
| 51 | static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino); |
| 52 | static int check_dir_block(ext2_filsys fs, |
| 53 | struct dir_block_struct *dir_blocks_info, |
| 54 | char *buf); |
| 55 | |
| 56 | void pass2(ext2_filsys fs) |
| 57 | { |
| 58 | int i; |
| 59 | char *buf; |
| 60 | struct resource_track rtrack; |
| 61 | |
| 62 | init_resource_track(&rtrack); |
| 63 | |
| 64 | #ifdef MTRACE |
| 65 | mtrace_print("Pass 2"); |
| 66 | #endif |
| 67 | |
| 68 | if (!preen) |
| 69 | printf("Pass 2: Checking directory structure\n"); |
| 70 | inode_count = allocate_memory((fs->super->s_inodes_count + 1) * |
| 71 | sizeof(unsigned short), |
| 72 | "buffer for inode count"); |
| 73 | |
| 74 | buf = allocate_memory(fs->blocksize, "directory scan buffer"); |
| 75 | |
| 76 | for (i=0; i < dir_block_count; i++) |
| 77 | check_dir_block(fs, &dir_blocks[i], buf); |
| 78 | |
| 79 | free(buf); |
| 80 | free(dir_blocks); |
| 81 | if (inode_bad_map) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 82 | ext2fs_free_inode_bitmap(inode_bad_map); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 83 | inode_bad_map = 0; |
| 84 | } |
| 85 | if (tflag > 1) { |
| 86 | printf("Pass 2: "); |
| 87 | print_resource_track(&rtrack); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Make sure the first entry in the directory is '.', and that the |
| 93 | * directory entry is sane. |
| 94 | */ |
| 95 | static int check_dot(ext2_filsys fs, |
| 96 | struct ext2_dir_entry *dirent, |
| 97 | ino_t ino) |
| 98 | { |
| 99 | struct ext2_dir_entry *nextdir; |
| 100 | int status = 0; |
| 101 | int created = 0; |
| 102 | int new_len; |
| 103 | char name[BLOCK_SIZE]; |
| 104 | |
| 105 | if (!dirent->inode) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 106 | printf("Missing '.' in directory inode %lu.\n", ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 107 | if (dirent->rec_len < 12) |
| 108 | fatal_error("Cannot fix, insufficient space to add '.'"); |
| 109 | preenhalt(); |
| 110 | if (ask("Fix", 1)) { |
| 111 | dirent->inode = ino; |
| 112 | dirent->name_len = 1; |
| 113 | dirent->name[0] = '.'; |
| 114 | status = 1; |
| 115 | created = 1; |
| 116 | } else { |
| 117 | ext2fs_unmark_valid(fs); |
| 118 | return 0; |
| 119 | } |
| 120 | } |
| 121 | if ((dirent->name_len != 1) || |
| 122 | strncmp(dirent->name, ".", dirent->name_len)) { |
| 123 | strncpy(name, dirent->name, dirent->name_len); |
| 124 | name[dirent->name_len] = '\0'; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 125 | printf("Missing '.' in directory inode %lu.\n", ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 126 | printf("Cannot fix, first entry in directory contains '%s'\n", |
| 127 | name); |
| 128 | exit(FSCK_ERROR); |
| 129 | } |
| 130 | if (dirent->inode != ino) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 131 | printf("Bad inode number for '.' in directory inode %lu.\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 132 | ino); |
| 133 | preenhalt(); |
| 134 | if (ask("Fix", 1)) { |
| 135 | dirent->inode = ino; |
| 136 | status = 1; |
| 137 | } else |
| 138 | ext2fs_unmark_valid(fs); |
| 139 | } |
| 140 | if (dirent->rec_len > 12) { |
| 141 | new_len = dirent->rec_len - 12; |
| 142 | if (new_len > 12) { |
| 143 | preenhalt(); |
| 144 | if (created || |
| 145 | ask("Directory entry for '.' is big. Split", 1)) { |
| 146 | nextdir = (struct ext2_dir_entry *) |
| 147 | ((char *) dirent + 12); |
| 148 | dirent->rec_len = 12; |
| 149 | nextdir->rec_len = new_len; |
| 150 | nextdir->inode = 0; |
| 151 | nextdir->name_len = 0; |
| 152 | status = 1; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | return status; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Make sure the second entry in the directory is '..', and that the |
| 161 | * directory entry is sane. We do not check the inode number of '..' |
| 162 | * here; this gets done in pass 3. |
| 163 | */ |
| 164 | static int check_dotdot(ext2_filsys fs, |
| 165 | struct ext2_dir_entry *dirent, |
| 166 | struct dir_info *dir) |
| 167 | { |
| 168 | char name[BLOCK_SIZE]; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 169 | ino_t ino = dir->ino; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 170 | |
| 171 | if (!dirent->inode) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 172 | printf("Missing '..' in directory inode %lu.\n", ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 173 | if (dirent->rec_len < 12) |
| 174 | fatal_error("Cannot fix, insufficient space to add '..'"); |
| 175 | preenhalt(); |
| 176 | if (ask("Fix", 1)) { |
| 177 | /* |
| 178 | * Note: we don't have the parent inode just |
| 179 | * yet, so we will fill it in with the root |
| 180 | * inode. This will get fixed in pass 3. |
| 181 | */ |
| 182 | dirent->inode = EXT2_ROOT_INO; |
| 183 | dirent->name_len = 2; |
| 184 | dirent->name[0] = '.'; |
| 185 | dirent->name[1] = '.'; |
| 186 | return 1; |
| 187 | } else |
| 188 | ext2fs_unmark_valid(fs); |
| 189 | return 0; |
| 190 | } |
| 191 | if ((dirent->name_len != 2) || |
| 192 | strncmp(dirent->name, "..", dirent->name_len)) { |
| 193 | strncpy(name, dirent->name, dirent->name_len); |
| 194 | name[dirent->name_len] = '\0'; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 195 | printf("Missing '..' in directory inode %lu.\n", ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 196 | printf("Cannot fix, first entry in directory contains %s\n", |
| 197 | name); |
| 198 | exit(FSCK_ERROR); |
| 199 | } |
| 200 | dir->dotdot = dirent->inode; |
| 201 | return 0; |
| 202 | } |
| 203 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 204 | static char unknown_pathname[] = "???"; |
| 205 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 206 | /* |
| 207 | * Check to make sure a directory entry doesn't contain any illegal |
| 208 | * characters. |
| 209 | */ |
| 210 | static int check_name(ext2_filsys fs, |
| 211 | struct ext2_dir_entry *dirent, |
| 212 | ino_t dir_ino, |
| 213 | char *name) |
| 214 | { |
| 215 | int i; |
| 216 | int fixup = -1; |
| 217 | char *pathname; |
| 218 | int ret = 0; |
| 219 | errcode_t retval; |
| 220 | |
| 221 | for ( i = 0; i < dirent->name_len; i++) { |
| 222 | if (dirent->name[i] == '/' || dirent->name[i] == '\0') { |
| 223 | if (fixup < 0) { |
| 224 | retval = ext2fs_get_pathname(fs, dir_ino, |
| 225 | 0, &pathname); |
| 226 | if (retval) { |
| 227 | com_err(program_name, retval, "while getting pathname in check_name"); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 228 | pathname = unknown_pathname; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 229 | } |
| 230 | printf ("Bad file name '%s' (contains '/' or " |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 231 | " null) in directory '%s' (%lu)\n", |
| 232 | name, pathname, dir_ino); |
| 233 | if (pathname != unknown_pathname) |
| 234 | free(pathname); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 235 | preenhalt(); |
| 236 | fixup = ask("Replace '/' or null by '.'", 1); |
| 237 | } |
| 238 | if (fixup) { |
| 239 | dirent->name[i] = '.'; |
| 240 | ret = 1; |
| 241 | } else |
| 242 | ext2fs_unmark_valid(fs); |
| 243 | } |
| 244 | } |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | static int check_dir_block(ext2_filsys fs, |
| 249 | struct dir_block_struct *db, |
| 250 | char *buf) |
| 251 | { |
| 252 | struct dir_info *subdir, *dir; |
| 253 | struct ext2_dir_entry *dirent; |
| 254 | char name[BLOCK_SIZE]; |
| 255 | int offset = 0; |
| 256 | int dir_modified = 0; |
| 257 | errcode_t retval; |
| 258 | char *path1, *path2; |
| 259 | int dot_state; |
| 260 | blk_t block_nr = db->blk; |
| 261 | ino_t ino = db->ino; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * Make sure the inode is still in use (could have been |
| 265 | * deleted in the duplicate/bad blocks pass. |
| 266 | */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 267 | if (!(ext2fs_test_inode_bitmap(inode_used_map, ino))) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 268 | return 0; |
| 269 | |
| 270 | if (db->blockcnt) |
| 271 | dot_state = 2; |
| 272 | else |
| 273 | dot_state = 0; |
| 274 | |
| 275 | #if 0 |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 276 | 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] | 277 | db->blockcnt, ino); |
| 278 | #endif |
| 279 | |
| 280 | retval = io_channel_read_blk(fs->io, block_nr, 1, buf); |
| 281 | if (retval) { |
| 282 | com_err(program_name, retval, |
| 283 | "while reading directory block %d", block_nr); |
| 284 | } |
| 285 | |
| 286 | do { |
| 287 | dot_state++; |
| 288 | dirent = (struct ext2_dir_entry *) (buf + offset); |
| 289 | if (((offset + dirent->rec_len) > fs->blocksize) || |
| 290 | (dirent->rec_len < 8) || |
| 291 | ((dirent->name_len+8) > dirent->rec_len)) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 292 | printf("Directory inode %lu, block %d, offset %d: directory corrupted\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 293 | ino, db->blockcnt, offset); |
| 294 | preenhalt(); |
| 295 | if (ask("Salvage", 1)) { |
| 296 | dirent->rec_len = fs->blocksize - offset; |
| 297 | dirent->name_len = 0; |
| 298 | dirent->inode = 0; |
| 299 | dir_modified++; |
| 300 | } else { |
| 301 | ext2fs_unmark_valid(fs); |
| 302 | return DIRENT_ABORT; |
| 303 | } |
| 304 | } |
| 305 | strncpy(name, dirent->name, dirent->name_len); |
| 306 | name[dirent->name_len] = '\0'; |
| 307 | if (dot_state == 1) { |
| 308 | if (check_dot(fs, dirent, ino)) |
| 309 | dir_modified++; |
| 310 | } else if (dot_state == 2) { |
| 311 | dir = get_dir_info(ino); |
| 312 | if (!dir) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 313 | printf("Internal error: couldn't find dir_info for %lu\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 314 | ino); |
| 315 | fatal_error(0); |
| 316 | } |
| 317 | if (check_dotdot(fs, dirent, dir)) |
| 318 | dir_modified++; |
| 319 | } else if (dirent->inode == ino) { |
| 320 | retval = ext2fs_get_pathname(fs, ino, 0, &path1); |
| 321 | if (retval) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 322 | path1 = unknown_pathname; |
| 323 | printf("Entry '%s' in %s (%lu) is a link to '.' ", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 324 | name, path1, ino); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 325 | if (path1 != unknown_pathname) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 326 | free(path1); |
| 327 | preenhalt(); |
| 328 | if (ask("Clear", 1)) { |
| 329 | dirent->inode = 0; |
| 330 | dir_modified++; |
| 331 | } |
| 332 | } |
| 333 | if (!dirent->inode) |
| 334 | goto next; |
| 335 | |
| 336 | #if 0 |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 337 | printf("Entry '%s', name_len %d, rec_len %d, inode %lu... ", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 338 | name, dirent->name_len, dirent->rec_len, dirent->inode); |
| 339 | #endif |
| 340 | if (check_name(fs, dirent, ino, name)) |
| 341 | dir_modified++; |
| 342 | |
| 343 | /* |
| 344 | * Make sure the inode listed is a legal one. |
| 345 | */ |
| 346 | if (((dirent->inode != EXT2_ROOT_INO) && |
| 347 | (dirent->inode < EXT2_FIRST_INO)) || |
| 348 | (dirent->inode > fs->super->s_inodes_count)) { |
| 349 | retval = ext2fs_get_pathname(fs, ino, 0, &path1); |
| 350 | if (retval) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 351 | path1 = unknown_pathname; |
| 352 | printf("Entry '%s' in %s (%lu) has bad inode #: %lu.\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 353 | name, path1, ino, dirent->inode); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 354 | if (path1 != unknown_pathname) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 355 | free(path1); |
| 356 | preenhalt(); |
| 357 | if (ask("Clear", 1)) { |
| 358 | dirent->inode = 0; |
| 359 | dir_modified++; |
| 360 | goto next; |
| 361 | } else |
| 362 | ext2fs_unmark_valid(fs); |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * If the inode is unusued, offer to clear it. |
| 367 | */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 368 | if (!(ext2fs_test_inode_bitmap(inode_used_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 369 | dirent->inode))) { |
| 370 | retval = ext2fs_get_pathname(fs, ino, 0, &path1); |
| 371 | if (retval) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 372 | path1 = unknown_pathname; |
| 373 | printf("Entry '%s' in %s (%lu) has deleted/unused inode %lu.\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 374 | name, path1, ino, dirent->inode); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 375 | if (path1 != unknown_pathname) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 376 | free(path1); |
| 377 | if (ask("Clear", 1)) { |
| 378 | dirent->inode = 0; |
| 379 | dir_modified++; |
| 380 | goto next; |
| 381 | } else |
| 382 | ext2fs_unmark_valid(fs); |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * If the inode was marked as having bad fields in |
| 387 | * pass1, process it and offer to fix/clear it. |
| 388 | * (We wait until now so that we can display the |
| 389 | * pathname to the user.) |
| 390 | */ |
| 391 | if (inode_bad_map && |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 392 | ext2fs_test_inode_bitmap(inode_bad_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 393 | dirent->inode)) { |
| 394 | if (process_bad_inode(fs, ino, dirent->inode)) { |
| 395 | dirent->inode = 0; |
| 396 | dir_modified++; |
| 397 | goto next; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * If this is a directory, then mark its parent in its |
| 403 | * dir_info structure. If the parent field is already |
| 404 | * filled in, then this directory has more than one |
| 405 | * hard link. We assume the first link is correct, |
| 406 | * and ask the user if he/she wants to clear this one. |
| 407 | */ |
| 408 | if ((dot_state > 2) && |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 409 | (ext2fs_test_inode_bitmap(inode_dir_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 410 | dirent->inode))) { |
| 411 | subdir = get_dir_info(dirent->inode); |
| 412 | if (!subdir) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 413 | printf("INTERNAL ERROR: missing dir %lu\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 414 | dirent->inode); |
| 415 | fatal_error(0); |
| 416 | } |
| 417 | if (subdir->parent) { |
| 418 | retval = ext2fs_get_pathname(fs, ino, |
| 419 | 0, &path1); |
| 420 | if (retval) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 421 | path1 = unknown_pathname; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 422 | retval = ext2fs_get_pathname(fs, |
| 423 | subdir->parent, |
| 424 | dirent->inode, |
| 425 | &path2); |
| 426 | if (retval) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 427 | path2 = unknown_pathname; |
| 428 | printf("Entry '%s' in %s (%lu) is a link to directory %s (%lu).\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 429 | name, path1, ino, path2, |
| 430 | dirent->inode); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 431 | if (path1 != unknown_pathname) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 432 | free(path1); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 433 | if (path2 != unknown_pathname) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 434 | free(path2); |
| 435 | if (ask("Clear", 1)) { |
| 436 | dirent->inode = 0; |
| 437 | dir_modified++; |
| 438 | goto next; |
| 439 | } else |
| 440 | ext2fs_unmark_valid(fs); |
| 441 | } |
| 442 | subdir->parent = ino; |
| 443 | } |
| 444 | |
| 445 | if (inode_count[dirent->inode]++ > 0) |
| 446 | fs_links_count++; |
| 447 | fs_total_count++; |
| 448 | next: |
| 449 | offset += dirent->rec_len; |
| 450 | } while (offset < fs->blocksize); |
| 451 | #if 0 |
| 452 | printf("\n"); |
| 453 | #endif |
| 454 | if (offset != fs->blocksize) { |
| 455 | printf("Final rec_len is %d, should be %d\n", |
| 456 | dirent->rec_len, |
| 457 | dirent->rec_len - fs->blocksize + offset); |
| 458 | } |
| 459 | if (dir_modified) { |
| 460 | retval = io_channel_write_blk(fs->io, block_nr, |
| 461 | 1, buf); |
| 462 | if (retval) { |
| 463 | com_err(program_name, retval, |
| 464 | "while writing directory block %d", block_nr); |
| 465 | } |
| 466 | ext2fs_mark_changed(fs); |
| 467 | } |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * This function is called to deallocate a block, and is an interator |
| 473 | * functioned called by deallocate inode via ext2fs_iterate_block(). |
| 474 | */ |
| 475 | static int deallocate_inode_block(ext2_filsys fs, |
| 476 | blk_t *block_nr, |
| 477 | int blockcnt, |
| 478 | void *private) |
| 479 | { |
| 480 | if (!*block_nr) |
| 481 | return 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 482 | ext2fs_unmark_block_bitmap(block_found_map, *block_nr); |
| 483 | ext2fs_unmark_block_bitmap(fs->block_map, *block_nr); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | /* |
| 488 | * This fuction deallocates an inode |
| 489 | */ |
| 490 | static void deallocate_inode(ext2_filsys fs, ino_t ino, |
| 491 | char* block_buf) |
| 492 | { |
| 493 | errcode_t retval; |
| 494 | struct ext2_inode inode; |
| 495 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 496 | e2fsck_read_inode(fs, ino, &inode, "deallocate_inode"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 497 | inode.i_links_count = 0; |
| 498 | inode.i_dtime = time(0); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 499 | e2fsck_write_inode(fs, ino, &inode, "deallocate_inode"); |
| 500 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 501 | /* |
| 502 | * Fix up the bitmaps... |
| 503 | */ |
| 504 | read_bitmaps(fs); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 505 | ext2fs_unmark_inode_bitmap(inode_used_map, ino); |
| 506 | ext2fs_unmark_inode_bitmap(inode_dir_map, ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 507 | if (inode_bad_map) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 508 | ext2fs_unmark_inode_bitmap(inode_bad_map, ino); |
| 509 | ext2fs_unmark_inode_bitmap(fs->inode_map, ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 510 | ext2fs_mark_ib_dirty(fs); |
| 511 | |
| 512 | if (!inode_has_valid_blocks(&inode)) |
| 513 | return; |
| 514 | |
| 515 | ext2fs_mark_bb_dirty(fs); |
| 516 | retval = ext2fs_block_iterate(fs, ino, 0, block_buf, |
| 517 | deallocate_inode_block, 0); |
| 518 | if (retval) |
| 519 | com_err("deallocate_inode", retval, |
| 520 | "while calling ext2fs_block_iterate for inode %d", |
| 521 | ino); |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * These two subroutines are used by process_bad_inode; it is used to |
| 526 | * make sure that certain reserved fields are really zero. If not, |
| 527 | * prompt the user if he/she wants us to zeroize them. |
| 528 | */ |
| 529 | static void check_for_zero_long(ext2_filsys fs, ino_t ino, char *pathname, |
| 530 | const char *name, unsigned long *val, |
| 531 | int *modified) |
| 532 | { |
| 533 | char prompt[80]; |
| 534 | |
| 535 | if (*val) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 536 | printf("%s for inode %lu (%s) is %lu, should be zero.\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 537 | name, ino, pathname, *val); |
| 538 | preenhalt(); |
| 539 | sprintf(prompt, "Clear %s", name); |
| 540 | if (ask(prompt, 1)) { |
| 541 | *val = 0; |
| 542 | *modified = 1; |
| 543 | } else |
| 544 | ext2fs_unmark_valid(fs); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | static void check_for_zero_char(ext2_filsys fs, ino_t ino, char *pathname, |
| 549 | const char *name, unsigned char *val, |
| 550 | int *modified) |
| 551 | { |
| 552 | char prompt[80]; |
| 553 | |
| 554 | if (*val) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 555 | printf("%s for inode %lu (%s) is %d, should be zero.\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 556 | name, ino, pathname, *val); |
| 557 | preenhalt(); |
| 558 | sprintf(prompt, "Clear %s", name); |
| 559 | if (ask(prompt, 1)) { |
| 560 | *val = 0; |
| 561 | *modified = 1; |
| 562 | } else |
| 563 | ext2fs_unmark_valid(fs); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | |
| 568 | |
| 569 | static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino) |
| 570 | { |
| 571 | struct ext2_inode inode; |
| 572 | errcode_t retval; |
| 573 | int inode_modified = 0; |
| 574 | char *pathname; |
| 575 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 576 | e2fsck_read_inode(fs, ino, &inode, "process_bad_inode"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 577 | retval = ext2fs_get_pathname(fs, dir, ino, &pathname); |
| 578 | if (retval) { |
| 579 | com_err("process_bad_inode", retval, |
| 580 | "while getting pathname for inode %d", |
| 581 | ino); |
| 582 | return 0; |
| 583 | } |
| 584 | if (!S_ISDIR(inode.i_mode) && !S_ISREG(inode.i_mode) && |
| 585 | !S_ISCHR(inode.i_mode) && !S_ISBLK(inode.i_mode) && |
| 586 | !S_ISLNK(inode.i_mode) && !S_ISFIFO(inode.i_mode) && |
| 587 | !(S_ISSOCK(inode.i_mode))) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 588 | printf("Inode %lu (%s) has a bad mode (0%o).\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 589 | ino, pathname, inode.i_mode); |
| 590 | preenhalt(); |
| 591 | if (ask("Clear", 1)) { |
| 592 | deallocate_inode(fs, ino, 0); |
| 593 | return 1; |
| 594 | } else |
| 595 | ext2fs_unmark_valid(fs); |
| 596 | } |
| 597 | check_for_zero_long(fs, ino, pathname, "i_faddr", &inode.i_faddr, |
| 598 | &inode_modified); |
| 599 | check_for_zero_char(fs, ino, pathname, "i_frag", &inode.i_frag, |
| 600 | &inode_modified); |
| 601 | check_for_zero_char(fs, ino, pathname, "i_fsize", &inode.i_fsize, |
| 602 | &inode_modified); |
| 603 | check_for_zero_long(fs, ino, pathname, "i_file_acl", &inode.i_file_acl, |
| 604 | &inode_modified); |
| 605 | check_for_zero_long(fs, ino, pathname, "i_dir_acl", &inode.i_dir_acl, |
| 606 | &inode_modified); |
| 607 | free(pathname); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 608 | if (inode_modified) |
| 609 | e2fsck_write_inode(fs, ino, &inode, "process_bad_inode"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 610 | return 0; |
| 611 | } |
| 612 | |