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