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