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