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