]>
Commit | Line | Data |
---|---|---|
3839e657 TT |
1 | /* |
2 | * pass2.c --- check directory structure | |
efc6f628 | 3 | * |
21c84b71 TT |
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% | |
efc6f628 | 10 | * |
3839e657 TT |
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) | |
efc6f628 | 19 | * should be less than (rec_len - 8). |
3839e657 TT |
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 | |
3839e657 TT |
38 | * |
39 | * Pass 2 frees the following data structures | |
40 | * - The inode_bad_map bitmap | |
aa4115a4 | 41 | * - The inode_reg_map bitmap |
3839e657 TT |
42 | */ |
43 | ||
b969b1b8 | 44 | #define _GNU_SOURCE 1 /* get strnlen() */ |
48e6e813 TT |
45 | #include <string.h> |
46 | ||
3839e657 | 47 | #include "e2fsck.h" |
21c84b71 | 48 | #include "problem.h" |
0926668d | 49 | #include "dict.h" |
3839e657 | 50 | |
aa4115a4 TT |
51 | #ifdef NO_INLINE_FUNCS |
52 | #define _INLINE_ | |
53 | #else | |
54 | #define _INLINE_ inline | |
55 | #endif | |
56 | ||
ad4fa466 | 57 | /* #define DX_DEBUG */ |
8fdc9985 | 58 | |
3839e657 TT |
59 | /* |
60 | * Keeps track of how many times an inode is referenced. | |
61 | */ | |
4035f40b | 62 | static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf); |
3839e657 | 63 | static int check_dir_block(ext2_filsys fs, |
21c84b71 | 64 | struct ext2_db_entry *dir_blocks_info, |
54dc7ca2 | 65 | void *priv_data); |
1b6bf175 | 66 | static int allocate_dir_block(e2fsck_t ctx, |
21c84b71 TT |
67 | struct ext2_db_entry *dir_blocks_info, |
68 | char *buf, struct problem_context *pctx); | |
50e1e10f TT |
69 | static int update_dir_block(ext2_filsys fs, |
70 | blk_t *block_nr, | |
133a56dc | 71 | e2_blkcnt_t blockcnt, |
4035f40b | 72 | blk_t ref_block, |
efc6f628 | 73 | int ref_offset, |
4035f40b | 74 | void *priv_data); |
8fdc9985 | 75 | static void clear_htree(e2fsck_t ctx, ext2_ino_t ino); |
ad4fa466 TT |
76 | static int htree_depth(struct dx_dir_info *dx_dir, |
77 | struct dx_dirblock_info *dx_db); | |
ea1959f0 | 78 | static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b); |
3839e657 | 79 | |
21c84b71 TT |
80 | struct check_dir_struct { |
81 | char *buf; | |
82 | struct problem_context pctx; | |
f8188fff | 83 | int count, max; |
1b6bf175 | 84 | e2fsck_t ctx; |
efc6f628 | 85 | }; |
21c84b71 | 86 | |
08b21301 | 87 | void e2fsck_pass2(e2fsck_t ctx) |
3839e657 | 88 | { |
a4742691 TT |
89 | struct ext2_super_block *sb = ctx->fs->super; |
90 | struct problem_context pctx; | |
91 | ext2_filsys fs = ctx->fs; | |
92 | char *buf; | |
8bf191e8 | 93 | #ifdef RESOURCE_TRACK |
3839e657 | 94 | struct resource_track rtrack; |
8bf191e8 | 95 | #endif |
21c84b71 | 96 | struct check_dir_struct cd; |
8fdc9985 TT |
97 | struct dx_dir_info *dx_dir; |
98 | struct dx_dirblock_info *dx_db, *dx_parent; | |
54434927 | 99 | int b; |
ad4fa466 | 100 | int i, depth; |
8fdc9985 TT |
101 | problem_t code; |
102 | int bad_dir; | |
103 | ||
6d96b00d | 104 | init_resource_track(&rtrack, ctx->fs->io); |
1b6bf175 TT |
105 | clear_problem_context(&cd.pctx); |
106 | ||
3839e657 TT |
107 | #ifdef MTRACE |
108 | mtrace_print("Pass 2"); | |
109 | #endif | |
110 | ||
1b6bf175 TT |
111 | if (!(ctx->options & E2F_OPT_PREEN)) |
112 | fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx); | |
113 | ||
efc6f628 | 114 | e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT, |
34b9f796 TT |
115 | &ctx->inode_count); |
116 | if (ctx->inode_count) | |
117 | cd.pctx.errcode = 0; | |
efc6f628 TT |
118 | else |
119 | cd.pctx.errcode = ext2fs_create_icount2(fs, | |
34b9f796 | 120 | EXT2_ICOUNT_OPT_INCREMENT, |
1b6bf175 TT |
121 | 0, ctx->inode_link_info, |
122 | &ctx->inode_count); | |
123 | if (cd.pctx.errcode) { | |
124 | fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx); | |
08b21301 TT |
125 | ctx->flags |= E2F_FLAG_ABORT; |
126 | return; | |
21c84b71 | 127 | } |
bcf9c5d4 | 128 | buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize, |
54dc7ca2 | 129 | "directory scan buffer"); |
3839e657 | 130 | |
21c84b71 TT |
131 | /* |
132 | * Set up the parent pointer for the root directory, if | |
133 | * present. (If the root directory is not present, we will | |
134 | * create it in pass 3.) | |
135 | */ | |
28db82a8 | 136 | (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO); |
21c84b71 TT |
137 | |
138 | cd.buf = buf; | |
1b6bf175 | 139 | cd.ctx = ctx; |
f75c28de | 140 | cd.count = 1; |
f8188fff | 141 | cd.max = ext2fs_dblist_count(fs->dblist); |
f75c28de TT |
142 | |
143 | if (ctx->progress) | |
144 | (void) (ctx->progress)(ctx, 2, 0, cd.max); | |
ea1959f0 TT |
145 | |
146 | if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) | |
147 | ext2fs_dblist_sort(fs->dblist, special_dir_block_cmp); | |
efc6f628 | 148 | |
1b6bf175 TT |
149 | cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block, |
150 | &cd); | |
49a7360b | 151 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART) |
08b21301 | 152 | return; |
6267ee49 AD |
153 | |
154 | if (ctx->flags & E2F_FLAG_RESTART_LATER) { | |
155 | ctx->flags |= E2F_FLAG_RESTART; | |
156 | return; | |
157 | } | |
158 | ||
1b6bf175 TT |
159 | if (cd.pctx.errcode) { |
160 | fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx); | |
08b21301 TT |
161 | ctx->flags |= E2F_FLAG_ABORT; |
162 | return; | |
7ac02a5e | 163 | } |
8fdc9985 TT |
164 | |
165 | #ifdef ENABLE_HTREE | |
166 | for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) { | |
4cae0452 TT |
167 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
168 | return; | |
62acaa1d | 169 | if (dx_dir->numblocks == 0) |
8fdc9985 TT |
170 | continue; |
171 | clear_problem_context(&pctx); | |
172 | bad_dir = 0; | |
173 | pctx.dir = dx_dir->ino; | |
174 | dx_db = dx_dir->dx_block; | |
175 | if (dx_db->flags & DX_FLAG_REFERENCED) | |
176 | dx_db->flags |= DX_FLAG_DUP_REF; | |
177 | else | |
178 | dx_db->flags |= DX_FLAG_REFERENCED; | |
179 | /* | |
180 | * Find all of the first and last leaf blocks, and | |
181 | * update their parent's min and max hash values | |
182 | */ | |
183 | for (b=0, dx_db = dx_dir->dx_block; | |
184 | b < dx_dir->numblocks; | |
185 | b++, dx_db++) { | |
186 | if ((dx_db->type != DX_DIRBLOCK_LEAF) || | |
187 | !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST))) | |
188 | continue; | |
189 | dx_parent = &dx_dir->dx_block[dx_db->parent]; | |
190 | /* | |
191 | * XXX Make sure dx_parent->min_hash > dx_db->min_hash | |
192 | */ | |
193 | if (dx_db->flags & DX_FLAG_FIRST) | |
194 | dx_parent->min_hash = dx_db->min_hash; | |
195 | /* | |
196 | * XXX Make sure dx_parent->max_hash < dx_db->max_hash | |
197 | */ | |
198 | if (dx_db->flags & DX_FLAG_LAST) | |
199 | dx_parent->max_hash = dx_db->max_hash; | |
200 | } | |
efc6f628 | 201 | |
8fdc9985 TT |
202 | for (b=0, dx_db = dx_dir->dx_block; |
203 | b < dx_dir->numblocks; | |
204 | b++, dx_db++) { | |
205 | pctx.blkcount = b; | |
206 | pctx.group = dx_db->parent; | |
207 | code = 0; | |
208 | if (!(dx_db->flags & DX_FLAG_FIRST) && | |
209 | (dx_db->min_hash < dx_db->node_min_hash)) { | |
210 | pctx.blk = dx_db->min_hash; | |
211 | pctx.blk2 = dx_db->node_min_hash; | |
212 | code = PR_2_HTREE_MIN_HASH; | |
213 | fix_problem(ctx, code, &pctx); | |
214 | bad_dir++; | |
215 | } | |
ad4fa466 TT |
216 | if (dx_db->type == DX_DIRBLOCK_LEAF) { |
217 | depth = htree_depth(dx_dir, dx_db); | |
218 | if (depth != dx_dir->depth) { | |
e5e12db9 | 219 | pctx.num = dx_dir->depth; |
ad4fa466 TT |
220 | code = PR_2_HTREE_BAD_DEPTH; |
221 | fix_problem(ctx, code, &pctx); | |
222 | bad_dir++; | |
223 | } | |
224 | } | |
8fdc9985 | 225 | /* |
efc6f628 | 226 | * This test doesn't apply for the root block |
8fdc9985 TT |
227 | * at block #0 |
228 | */ | |
229 | if (b && | |
230 | (dx_db->max_hash > dx_db->node_max_hash)) { | |
231 | pctx.blk = dx_db->max_hash; | |
232 | pctx.blk2 = dx_db->node_max_hash; | |
233 | code = PR_2_HTREE_MAX_HASH; | |
234 | fix_problem(ctx, code, &pctx); | |
503f9e7f | 235 | bad_dir++; |
8fdc9985 TT |
236 | } |
237 | if (!(dx_db->flags & DX_FLAG_REFERENCED)) { | |
238 | code = PR_2_HTREE_NOTREF; | |
239 | fix_problem(ctx, code, &pctx); | |
240 | bad_dir++; | |
241 | } else if (dx_db->flags & DX_FLAG_DUP_REF) { | |
242 | code = PR_2_HTREE_DUPREF; | |
243 | fix_problem(ctx, code, &pctx); | |
244 | bad_dir++; | |
245 | } | |
8fdc9985 TT |
246 | } |
247 | if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) { | |
248 | clear_htree(ctx, dx_dir->ino); | |
62acaa1d | 249 | dx_dir->numblocks = 0; |
8fdc9985 | 250 | } |
8fdc9985 | 251 | } |
23f75f6e | 252 | e2fsck_free_dx_dir_info(ctx); |
8fdc9985 | 253 | #endif |
c4e3d3f3 | 254 | ext2fs_free_mem(&buf); |
21c84b71 TT |
255 | ext2fs_free_dblist(fs->dblist); |
256 | ||
1b6bf175 TT |
257 | if (ctx->inode_bad_map) { |
258 | ext2fs_free_inode_bitmap(ctx->inode_bad_map); | |
259 | ctx->inode_bad_map = 0; | |
3839e657 | 260 | } |
aa4115a4 TT |
261 | if (ctx->inode_reg_map) { |
262 | ext2fs_free_inode_bitmap(ctx->inode_reg_map); | |
263 | ctx->inode_reg_map = 0; | |
264 | } | |
a4742691 TT |
265 | |
266 | clear_problem_context(&pctx); | |
267 | if (ctx->large_files) { | |
268 | if (!(sb->s_feature_ro_compat & | |
269 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE) && | |
270 | fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) { | |
271 | sb->s_feature_ro_compat |= | |
272 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE; | |
0cfce7f7 | 273 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
a4742691 TT |
274 | ext2fs_mark_super_dirty(fs); |
275 | } | |
276 | if (sb->s_rev_level == EXT2_GOOD_OLD_REV && | |
277 | fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) { | |
278 | ext2fs_update_dynamic_rev(fs); | |
279 | ext2fs_mark_super_dirty(fs); | |
280 | } | |
a4742691 | 281 | } |
efc6f628 | 282 | |
9facd076 | 283 | print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io); |
3839e657 TT |
284 | } |
285 | ||
ad4fa466 TT |
286 | #define MAX_DEPTH 32000 |
287 | static int htree_depth(struct dx_dir_info *dx_dir, | |
288 | struct dx_dirblock_info *dx_db) | |
289 | { | |
290 | int depth = 0; | |
291 | ||
292 | while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) { | |
293 | dx_db = &dx_dir->dx_block[dx_db->parent]; | |
294 | depth++; | |
295 | } | |
296 | return depth; | |
297 | } | |
298 | ||
0926668d TT |
299 | static int dict_de_cmp(const void *a, const void *b) |
300 | { | |
520ead37 | 301 | const struct ext2_dir_entry *de_a, *de_b; |
0926668d TT |
302 | int a_len, b_len; |
303 | ||
520ead37 | 304 | de_a = (const struct ext2_dir_entry *) a; |
0926668d | 305 | a_len = de_a->name_len & 0xFF; |
520ead37 | 306 | de_b = (const struct ext2_dir_entry *) b; |
0926668d TT |
307 | b_len = de_b->name_len & 0xFF; |
308 | ||
309 | if (a_len != b_len) | |
310 | return (a_len - b_len); | |
311 | ||
312 | return strncmp(de_a->name, de_b->name, a_len); | |
313 | } | |
ad4fa466 | 314 | |
ea1959f0 TT |
315 | /* |
316 | * This is special sort function that makes sure that directory blocks | |
317 | * with a dirblock of zero are sorted to the beginning of the list. | |
318 | * This guarantees that the root node of the htree directories are | |
319 | * processed first, so we know what hash version to use. | |
320 | */ | |
321 | static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b) | |
322 | { | |
323 | const struct ext2_db_entry *db_a = | |
324 | (const struct ext2_db_entry *) a; | |
325 | const struct ext2_db_entry *db_b = | |
326 | (const struct ext2_db_entry *) b; | |
327 | ||
328 | if (db_a->blockcnt && !db_b->blockcnt) | |
329 | return 1; | |
330 | ||
331 | if (!db_a->blockcnt && db_b->blockcnt) | |
332 | return -1; | |
efc6f628 | 333 | |
ea1959f0 TT |
334 | if (db_a->blk != db_b->blk) |
335 | return (int) (db_a->blk - db_b->blk); | |
efc6f628 | 336 | |
ea1959f0 TT |
337 | if (db_a->ino != db_b->ino) |
338 | return (int) (db_a->ino - db_b->ino); | |
339 | ||
340 | return (int) (db_a->blockcnt - db_b->blockcnt); | |
341 | } | |
342 | ||
343 | ||
3839e657 TT |
344 | /* |
345 | * Make sure the first entry in the directory is '.', and that the | |
346 | * directory entry is sane. | |
347 | */ | |
1b6bf175 | 348 | static int check_dot(e2fsck_t ctx, |
3839e657 | 349 | struct ext2_dir_entry *dirent, |
86c627ec | 350 | ext2_ino_t ino, struct problem_context *pctx) |
3839e657 TT |
351 | { |
352 | struct ext2_dir_entry *nextdir; | |
8a480350 | 353 | unsigned int rec_len, new_len; |
3839e657 TT |
354 | int status = 0; |
355 | int created = 0; | |
21c84b71 | 356 | int problem = 0; |
efc6f628 | 357 | |
21c84b71 TT |
358 | if (!dirent->inode) |
359 | problem = PR_2_MISSING_DOT; | |
b6f79831 | 360 | else if (((dirent->name_len & 0xFF) != 1) || |
21c84b71 TT |
361 | (dirent->name[0] != '.')) |
362 | problem = PR_2_1ST_NOT_DOT; | |
363 | else if (dirent->name[1] != '\0') | |
364 | problem = PR_2_DOT_NULL_TERM; | |
5dd77dbe | 365 | |
8a480350 | 366 | (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len); |
21c84b71 | 367 | if (problem) { |
1b6bf175 | 368 | if (fix_problem(ctx, problem, pctx)) { |
5dd77dbe TT |
369 | if (rec_len < 12) |
370 | rec_len = dirent->rec_len = 12; | |
3839e657 TT |
371 | dirent->inode = ino; |
372 | dirent->name_len = 1; | |
373 | dirent->name[0] = '.'; | |
21c84b71 | 374 | dirent->name[1] = '\0'; |
3839e657 TT |
375 | status = 1; |
376 | created = 1; | |
3839e657 TT |
377 | } |
378 | } | |
3839e657 | 379 | if (dirent->inode != ino) { |
1b6bf175 | 380 | if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) { |
3839e657 TT |
381 | dirent->inode = ino; |
382 | status = 1; | |
21c84b71 | 383 | } |
3839e657 | 384 | } |
5dd77dbe TT |
385 | if (rec_len > 12) { |
386 | new_len = rec_len - 12; | |
3839e657 | 387 | if (new_len > 12) { |
3839e657 | 388 | if (created || |
f8188fff | 389 | fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) { |
3839e657 TT |
390 | nextdir = (struct ext2_dir_entry *) |
391 | ((char *) dirent + 12); | |
392 | dirent->rec_len = 12; | |
8a480350 TT |
393 | (void) ext2fs_set_rec_len(ctx->fs, new_len, |
394 | nextdir); | |
3839e657 TT |
395 | nextdir->inode = 0; |
396 | nextdir->name_len = 0; | |
397 | status = 1; | |
398 | } | |
399 | } | |
400 | } | |
401 | return status; | |
402 | } | |
403 | ||
404 | /* | |
405 | * Make sure the second entry in the directory is '..', and that the | |
406 | * directory entry is sane. We do not check the inode number of '..' | |
407 | * here; this gets done in pass 3. | |
408 | */ | |
1b6bf175 | 409 | static int check_dotdot(e2fsck_t ctx, |
3839e657 | 410 | struct ext2_dir_entry *dirent, |
28db82a8 | 411 | ext2_ino_t ino, struct problem_context *pctx) |
3839e657 | 412 | { |
5dd77dbe | 413 | int rec_len, problem = 0; |
efc6f628 | 414 | |
21c84b71 TT |
415 | if (!dirent->inode) |
416 | problem = PR_2_MISSING_DOT_DOT; | |
b6f79831 | 417 | else if (((dirent->name_len & 0xFF) != 2) || |
21c84b71 TT |
418 | (dirent->name[0] != '.') || |
419 | (dirent->name[1] != '.')) | |
420 | problem = PR_2_2ND_NOT_DOT_DOT; | |
421 | else if (dirent->name[2] != '\0') | |
422 | problem = PR_2_DOT_DOT_NULL_TERM; | |
423 | ||
8a480350 | 424 | (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len); |
21c84b71 | 425 | if (problem) { |
1b6bf175 | 426 | if (fix_problem(ctx, problem, pctx)) { |
5dd77dbe | 427 | if (rec_len < 12) |
21c84b71 | 428 | dirent->rec_len = 12; |
3839e657 TT |
429 | /* |
430 | * Note: we don't have the parent inode just | |
431 | * yet, so we will fill it in with the root | |
432 | * inode. This will get fixed in pass 3. | |
433 | */ | |
434 | dirent->inode = EXT2_ROOT_INO; | |
435 | dirent->name_len = 2; | |
436 | dirent->name[0] = '.'; | |
437 | dirent->name[1] = '.'; | |
21c84b71 | 438 | dirent->name[2] = '\0'; |
3839e657 | 439 | return 1; |
efc6f628 | 440 | } |
3839e657 TT |
441 | return 0; |
442 | } | |
28db82a8 TT |
443 | if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) { |
444 | fix_problem(ctx, PR_2_NO_DIRINFO, pctx); | |
445 | return -1; | |
446 | } | |
3839e657 TT |
447 | return 0; |
448 | } | |
449 | ||
450 | /* | |
451 | * Check to make sure a directory entry doesn't contain any illegal | |
452 | * characters. | |
453 | */ | |
1b6bf175 | 454 | static int check_name(e2fsck_t ctx, |
3839e657 | 455 | struct ext2_dir_entry *dirent, |
efc6f628 | 456 | ext2_ino_t dir_ino EXT2FS_ATTR((unused)), |
54434927 | 457 | struct problem_context *pctx) |
3839e657 TT |
458 | { |
459 | int i; | |
460 | int fixup = -1; | |
3839e657 | 461 | int ret = 0; |
efc6f628 | 462 | |
b6f79831 | 463 | for ( i = 0; i < (dirent->name_len & 0xFF); i++) { |
3839e657 TT |
464 | if (dirent->name[i] == '/' || dirent->name[i] == '\0') { |
465 | if (fixup < 0) { | |
1b6bf175 | 466 | fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx); |
3839e657 TT |
467 | } |
468 | if (fixup) { | |
469 | dirent->name[i] = '.'; | |
470 | ret = 1; | |
21c84b71 | 471 | } |
3839e657 TT |
472 | } |
473 | } | |
474 | return ret; | |
475 | } | |
476 | ||
aa4115a4 TT |
477 | /* |
478 | * Check the directory filetype (if present) | |
479 | */ | |
480 | static _INLINE_ int check_filetype(e2fsck_t ctx, | |
54434927 TT |
481 | struct ext2_dir_entry *dirent, |
482 | ext2_ino_t dir_ino EXT2FS_ATTR((unused)), | |
483 | struct problem_context *pctx) | |
aa4115a4 TT |
484 | { |
485 | int filetype = dirent->name_len >> 8; | |
486 | int should_be = EXT2_FT_UNKNOWN; | |
487 | struct ext2_inode inode; | |
488 | ||
489 | if (!(ctx->fs->super->s_feature_incompat & | |
7847c1d4 TT |
490 | EXT2_FEATURE_INCOMPAT_FILETYPE)) { |
491 | if (filetype == 0 || | |
492 | !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx)) | |
493 | return 0; | |
494 | dirent->name_len = dirent->name_len & 0xFF; | |
495 | return 1; | |
496 | } | |
aa4115a4 | 497 | |
c5d2f50d | 498 | if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) { |
aa4115a4 | 499 | should_be = EXT2_FT_DIR; |
c5d2f50d | 500 | } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map, |
aa4115a4 TT |
501 | dirent->inode)) { |
502 | should_be = EXT2_FT_REG_FILE; | |
503 | } else if (ctx->inode_bad_map && | |
c5d2f50d | 504 | ext2fs_test_inode_bitmap2(ctx->inode_bad_map, |
aa4115a4 TT |
505 | dirent->inode)) |
506 | should_be = 0; | |
507 | else { | |
508 | e2fsck_read_inode(ctx, dirent->inode, &inode, | |
509 | "check_filetype"); | |
6fdc7a32 | 510 | should_be = ext2_file_type(inode.i_mode); |
aa4115a4 TT |
511 | } |
512 | if (filetype == should_be) | |
513 | return 0; | |
514 | pctx->num = should_be; | |
515 | ||
516 | if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE, | |
517 | pctx) == 0) | |
518 | return 0; | |
efc6f628 | 519 | |
aa4115a4 TT |
520 | dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8; |
521 | return 1; | |
522 | } | |
523 | ||
8fdc9985 TT |
524 | #ifdef ENABLE_HTREE |
525 | static void parse_int_node(ext2_filsys fs, | |
526 | struct ext2_db_entry *db, | |
527 | struct check_dir_struct *cd, | |
528 | struct dx_dir_info *dx_dir, | |
529 | char *block_buf) | |
530 | { | |
531 | struct ext2_dx_root_info *root; | |
532 | struct ext2_dx_entry *ent; | |
533 | struct ext2_dx_countlimit *limit; | |
534 | struct dx_dirblock_info *dx_db; | |
ad4fa466 | 535 | int i, expect_limit, count; |
8fdc9985 TT |
536 | blk_t blk; |
537 | ext2_dirhash_t min_hash = 0xffffffff; | |
538 | ext2_dirhash_t max_hash = 0; | |
ad4fa466 | 539 | ext2_dirhash_t hash = 0, prev_hash; |
8fdc9985 TT |
540 | |
541 | if (db->blockcnt == 0) { | |
542 | root = (struct ext2_dx_root_info *) (block_buf + 24); | |
efc6f628 | 543 | |
8fdc9985 TT |
544 | #ifdef DX_DEBUG |
545 | printf("Root node dump:\n"); | |
8deb80a5 | 546 | printf("\t Reserved zero: %u\n", root->reserved_zero); |
8fdc9985 TT |
547 | printf("\t Hash Version: %d\n", root->hash_version); |
548 | printf("\t Info length: %d\n", root->info_length); | |
549 | printf("\t Indirect levels: %d\n", root->indirect_levels); | |
550 | printf("\t Flags: %d\n", root->unused_flags); | |
551 | #endif | |
552 | ||
553 | ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length); | |
554 | } else { | |
555 | ent = (struct ext2_dx_entry *) (block_buf+8); | |
556 | } | |
557 | limit = (struct ext2_dx_countlimit *) ent; | |
558 | ||
559 | #ifdef DX_DEBUG | |
efc6f628 | 560 | printf("Number of entries (count): %d\n", |
8132d840 | 561 | ext2fs_le16_to_cpu(limit->count)); |
efc6f628 | 562 | printf("Number of entries (limit): %d\n", |
8132d840 | 563 | ext2fs_le16_to_cpu(limit->limit)); |
8fdc9985 TT |
564 | #endif |
565 | ||
8132d840 | 566 | count = ext2fs_le16_to_cpu(limit->count); |
ad4fa466 TT |
567 | expect_limit = (fs->blocksize - ((char *) ent - block_buf)) / |
568 | sizeof(struct ext2_dx_entry); | |
8132d840 TT |
569 | if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) { |
570 | cd->pctx.num = ext2fs_le16_to_cpu(limit->limit); | |
ad4fa466 TT |
571 | if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx)) |
572 | goto clear_and_exit; | |
573 | } | |
8132d840 TT |
574 | if (count > expect_limit) { |
575 | cd->pctx.num = count; | |
ad4fa466 TT |
576 | if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx)) |
577 | goto clear_and_exit; | |
578 | count = expect_limit; | |
579 | } | |
efc6f628 | 580 | |
ad4fa466 TT |
581 | for (i=0; i < count; i++) { |
582 | prev_hash = hash; | |
8132d840 | 583 | hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0; |
8fdc9985 | 584 | #ifdef DX_DEBUG |
8deb80a5 | 585 | printf("Entry #%d: Hash 0x%08x, block %u\n", i, |
8132d840 | 586 | hash, ext2fs_le32_to_cpu(ent[i].block)); |
8fdc9985 | 587 | #endif |
8132d840 | 588 | blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff; |
8fdc9985 | 589 | /* Check to make sure the block is valid */ |
977ac873 | 590 | if (blk >= (blk_t) dx_dir->numblocks) { |
b7a00563 | 591 | cd->pctx.blk = blk; |
8fdc9985 | 592 | if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK, |
ad4fa466 TT |
593 | &cd->pctx)) |
594 | goto clear_and_exit; | |
977ac873 | 595 | continue; |
8fdc9985 | 596 | } |
ad4fa466 TT |
597 | if (hash < prev_hash && |
598 | fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx)) | |
599 | goto clear_and_exit; | |
8fdc9985 TT |
600 | dx_db = &dx_dir->dx_block[blk]; |
601 | if (dx_db->flags & DX_FLAG_REFERENCED) { | |
602 | dx_db->flags |= DX_FLAG_DUP_REF; | |
603 | } else { | |
604 | dx_db->flags |= DX_FLAG_REFERENCED; | |
605 | dx_db->parent = db->blockcnt; | |
606 | } | |
607 | if (hash < min_hash) | |
608 | min_hash = hash; | |
609 | if (hash > max_hash) | |
610 | max_hash = hash; | |
611 | dx_db->node_min_hash = hash; | |
8132d840 | 612 | if ((i+1) < count) |
efc6f628 | 613 | dx_db->node_max_hash = |
8132d840 | 614 | ext2fs_le32_to_cpu(ent[i+1].hash) & ~1; |
8fdc9985 TT |
615 | else { |
616 | dx_db->node_max_hash = 0xfffffffe; | |
617 | dx_db->flags |= DX_FLAG_LAST; | |
618 | } | |
619 | if (i == 0) | |
620 | dx_db->flags |= DX_FLAG_FIRST; | |
621 | } | |
622 | #ifdef DX_DEBUG | |
623 | printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n", | |
624 | db->blockcnt, min_hash, max_hash); | |
625 | #endif | |
626 | dx_db = &dx_dir->dx_block[db->blockcnt]; | |
627 | dx_db->min_hash = min_hash; | |
628 | dx_db->max_hash = max_hash; | |
ad4fa466 TT |
629 | return; |
630 | ||
631 | clear_and_exit: | |
632 | clear_htree(cd->ctx, cd->pctx.ino); | |
633 | dx_dir->numblocks = 0; | |
8fdc9985 TT |
634 | } |
635 | #endif /* ENABLE_HTREE */ | |
aa4115a4 | 636 | |
e70ae99e TT |
637 | /* |
638 | * Given a busted directory, try to salvage it somehow. | |
efc6f628 | 639 | * |
e70ae99e | 640 | */ |
ad4fa466 | 641 | static void salvage_directory(ext2_filsys fs, |
e70ae99e TT |
642 | struct ext2_dir_entry *dirent, |
643 | struct ext2_dir_entry *prev, | |
54434927 | 644 | unsigned int *offset) |
e70ae99e TT |
645 | { |
646 | char *cp = (char *) dirent; | |
8a480350 TT |
647 | int left; |
648 | unsigned int rec_len, prev_rec_len; | |
642935c0 | 649 | unsigned int name_len = dirent->name_len & 0xFF; |
e70ae99e | 650 | |
8a480350 | 651 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); |
5dd77dbe TT |
652 | left = fs->blocksize - *offset - rec_len; |
653 | ||
e70ae99e TT |
654 | /* |
655 | * Special case of directory entry of size 8: copy what's left | |
656 | * of the directory block up to cover up the invalid hole. | |
657 | */ | |
5dd77dbe | 658 | if ((left >= 12) && (rec_len == 8)) { |
e70ae99e TT |
659 | memmove(cp, cp+8, left); |
660 | memset(cp + left, 0, 8); | |
ad4fa466 TT |
661 | return; |
662 | } | |
663 | /* | |
664 | * If the directory entry overruns the end of the directory | |
665 | * block, and the name is small enough to fit, then adjust the | |
666 | * record length. | |
667 | */ | |
668 | if ((left < 0) && | |
8a480350 TT |
669 | ((int) rec_len + left > 8) && |
670 | (name_len + 8 <= (int) rec_len + left) && | |
ad4fa466 TT |
671 | dirent->inode <= fs->super->s_inodes_count && |
672 | strnlen(dirent->name, name_len) == name_len) { | |
8a480350 | 673 | (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent); |
ad4fa466 | 674 | return; |
e70ae99e TT |
675 | } |
676 | /* | |
575307cc KS |
677 | * If the record length of the directory entry is a multiple |
678 | * of four, and not too big, such that it is valid, let the | |
679 | * previous directory entry absorb the invalid one. | |
e70ae99e | 680 | */ |
5dd77dbe TT |
681 | if (prev && rec_len && (rec_len % 4) == 0 && |
682 | (*offset + rec_len <= fs->blocksize)) { | |
8a480350 TT |
683 | (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len); |
684 | prev_rec_len += rec_len; | |
685 | (void) ext2fs_set_rec_len(fs, prev_rec_len, prev); | |
5dd77dbe | 686 | *offset += rec_len; |
ad4fa466 | 687 | return; |
e70ae99e TT |
688 | } |
689 | /* | |
690 | * Default salvage method --- kill all of the directory | |
691 | * entries for the rest of the block. We will either try to | |
692 | * absorb it into the previous directory entry, or create a | |
693 | * new empty directory entry the rest of the directory block. | |
694 | */ | |
695 | if (prev) { | |
8a480350 TT |
696 | (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len); |
697 | prev_rec_len += fs->blocksize - *offset; | |
698 | (void) ext2fs_set_rec_len(fs, prev_rec_len, prev); | |
ad4fa466 | 699 | *offset = fs->blocksize; |
e70ae99e | 700 | } else { |
8a480350 TT |
701 | rec_len = fs->blocksize - *offset; |
702 | (void) ext2fs_set_rec_len(fs, rec_len, dirent); | |
e70ae99e TT |
703 | dirent->name_len = 0; |
704 | dirent->inode = 0; | |
e70ae99e | 705 | } |
e70ae99e TT |
706 | } |
707 | ||
3839e657 | 708 | static int check_dir_block(ext2_filsys fs, |
21c84b71 | 709 | struct ext2_db_entry *db, |
54dc7ca2 | 710 | void *priv_data) |
3839e657 | 711 | { |
8fdc9985 TT |
712 | struct dx_dir_info *dx_dir; |
713 | #ifdef ENABLE_HTREE | |
714 | struct dx_dirblock_info *dx_db = 0; | |
715 | #endif /* ENABLE_HTREE */ | |
e70ae99e | 716 | struct ext2_dir_entry *dirent, *prev; |
8fdc9985 | 717 | ext2_dirhash_t hash; |
54434927 | 718 | unsigned int offset = 0; |
e94bc631 | 719 | const char * old_op; |
3839e657 | 720 | int dir_modified = 0; |
21c84b71 | 721 | int dot_state; |
03fa6f8a | 722 | unsigned int rec_len; |
3839e657 | 723 | blk_t block_nr = db->blk; |
86c627ec | 724 | ext2_ino_t ino = db->ino; |
28db82a8 | 725 | ext2_ino_t subdir_parent; |
21c84b71 | 726 | __u16 links; |
54dc7ca2 | 727 | struct check_dir_struct *cd; |
1b6bf175 TT |
728 | char *buf; |
729 | e2fsck_t ctx; | |
730 | int problem; | |
ea1959f0 | 731 | struct ext2_dx_root_info *root; |
e8254bfd | 732 | struct ext2_dx_countlimit *limit; |
0926668d TT |
733 | static dict_t de_dict; |
734 | struct problem_context pctx; | |
735 | int dups_found = 0; | |
28db82a8 | 736 | int ret; |
1b6bf175 | 737 | |
54dc7ca2 | 738 | cd = (struct check_dir_struct *) priv_data; |
1b6bf175 TT |
739 | buf = cd->buf; |
740 | ctx = cd->ctx; | |
f8188fff | 741 | |
49a7360b | 742 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART) |
4cae0452 | 743 | return DIRENT_ABORT; |
efc6f628 | 744 | |
4cae0452 TT |
745 | if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max)) |
746 | return DIRENT_ABORT; | |
efc6f628 | 747 | |
3839e657 | 748 | /* |
efc6f628 | 749 | * Make sure the inode is still in use (could have been |
3839e657 TT |
750 | * deleted in the duplicate/bad blocks pass. |
751 | */ | |
c5d2f50d | 752 | if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino))) |
3839e657 | 753 | return 0; |
50e1e10f | 754 | |
21c84b71 TT |
755 | cd->pctx.ino = ino; |
756 | cd->pctx.blk = block_nr; | |
757 | cd->pctx.blkcount = db->blockcnt; | |
758 | cd->pctx.ino2 = 0; | |
759 | cd->pctx.dirent = 0; | |
760 | cd->pctx.num = 0; | |
761 | ||
50e1e10f | 762 | if (db->blk == 0) { |
1b6bf175 | 763 | if (allocate_dir_block(ctx, db, buf, &cd->pctx)) |
50e1e10f TT |
764 | return 0; |
765 | block_nr = db->blk; | |
766 | } | |
efc6f628 | 767 | |
3839e657 TT |
768 | if (db->blockcnt) |
769 | dot_state = 2; | |
770 | else | |
771 | dot_state = 0; | |
772 | ||
0926668d TT |
773 | if (ctx->dirs_to_hash && |
774 | ext2fs_u32_list_test(ctx->dirs_to_hash, ino)) | |
775 | dups_found++; | |
776 | ||
3839e657 | 777 | #if 0 |
f3db3566 | 778 | printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr, |
3839e657 TT |
779 | db->blockcnt, ino); |
780 | #endif | |
efc6f628 | 781 | |
e94bc631 | 782 | old_op = ehandler_operation(_("reading directory block")); |
1b6bf175 | 783 | cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf); |
e94bc631 | 784 | ehandler_operation(0); |
b9852cd8 TT |
785 | if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED) |
786 | cd->pctx.errcode = 0; /* We'll handle this ourselves */ | |
1b6bf175 | 787 | if (cd->pctx.errcode) { |
08b21301 TT |
788 | if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) { |
789 | ctx->flags |= E2F_FLAG_ABORT; | |
790 | return DIRENT_ABORT; | |
791 | } | |
1b6bf175 | 792 | memset(buf, 0, fs->blocksize); |
3839e657 | 793 | } |
8fdc9985 TT |
794 | #ifdef ENABLE_HTREE |
795 | dx_dir = e2fsck_get_dx_dir_info(ctx, ino); | |
62acaa1d | 796 | if (dx_dir && dx_dir->numblocks) { |
8fdc9985 | 797 | if (db->blockcnt >= dx_dir->numblocks) { |
efc6f628 | 798 | if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK, |
d45edec0 TT |
799 | &pctx)) { |
800 | clear_htree(ctx, ino); | |
801 | dx_dir->numblocks = 0; | |
802 | dx_db = 0; | |
803 | goto out_htree; | |
804 | } | |
805 | fatal_error(ctx, _("Can not continue.")); | |
8fdc9985 TT |
806 | } |
807 | dx_db = &dx_dir->dx_block[db->blockcnt]; | |
808 | dx_db->type = DX_DIRBLOCK_LEAF; | |
809 | dx_db->phys = block_nr; | |
810 | dx_db->min_hash = ~0; | |
811 | dx_db->max_hash = 0; | |
efc6f628 | 812 | |
8fdc9985 | 813 | dirent = (struct ext2_dir_entry *) buf; |
8a480350 | 814 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); |
e8254bfd | 815 | limit = (struct ext2_dx_countlimit *) (buf+8); |
8fdc9985 | 816 | if (db->blockcnt == 0) { |
ea1959f0 | 817 | root = (struct ext2_dx_root_info *) (buf + 24); |
8fdc9985 TT |
818 | dx_db->type = DX_DIRBLOCK_ROOT; |
819 | dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST; | |
ea1959f0 TT |
820 | if ((root->reserved_zero || |
821 | root->info_length < 8 || | |
822 | root->indirect_levels > 1) && | |
823 | fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) { | |
824 | clear_htree(ctx, ino); | |
825 | dx_dir->numblocks = 0; | |
826 | dx_db = 0; | |
f77704e4 | 827 | } |
ea1959f0 | 828 | dx_dir->hashversion = root->hash_version; |
f77704e4 TT |
829 | if ((dx_dir->hashversion <= EXT2_HASH_TEA) && |
830 | (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH)) | |
831 | dx_dir->hashversion += 3; | |
ad4fa466 | 832 | dx_dir->depth = root->indirect_levels + 1; |
8fdc9985 | 833 | } else if ((dirent->inode == 0) && |
5dd77dbe | 834 | (rec_len == fs->blocksize) && |
e8254bfd | 835 | (dirent->name_len == 0) && |
efc6f628 TT |
836 | (ext2fs_le16_to_cpu(limit->limit) == |
837 | ((fs->blocksize-8) / | |
8132d840 | 838 | sizeof(struct ext2_dx_entry)))) |
8fdc9985 TT |
839 | dx_db->type = DX_DIRBLOCK_NODE; |
840 | } | |
d45edec0 | 841 | out_htree: |
8fdc9985 | 842 | #endif /* ENABLE_HTREE */ |
3839e657 | 843 | |
0926668d | 844 | dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp); |
e70ae99e | 845 | prev = 0; |
3839e657 | 846 | do { |
49a7360b JS |
847 | int group; |
848 | ext2_ino_t first_unused_inode; | |
849 | ||
1b6bf175 | 850 | problem = 0; |
3839e657 | 851 | dirent = (struct ext2_dir_entry *) (buf + offset); |
8a480350 | 852 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); |
21c84b71 TT |
853 | cd->pctx.dirent = dirent; |
854 | cd->pctx.num = offset; | |
5dd77dbe TT |
855 | if (((offset + rec_len) > fs->blocksize) || |
856 | (rec_len < 12) || | |
857 | ((rec_len % 4) != 0) || | |
03fa6f8a | 858 | (((dirent->name_len & (unsigned) 0xFF)+8) > rec_len)) { |
1b6bf175 | 859 | if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) { |
ad4fa466 | 860 | salvage_directory(fs, dirent, prev, &offset); |
3839e657 | 861 | dir_modified++; |
e70ae99e | 862 | continue; |
21c84b71 | 863 | } else |
0926668d | 864 | goto abort_free_dict; |
3839e657 | 865 | } |
b6f79831 | 866 | if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) { |
1b6bf175 | 867 | if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) { |
50e1e10f TT |
868 | dirent->name_len = EXT2_NAME_LEN; |
869 | dir_modified++; | |
870 | } | |
50e1e10f TT |
871 | } |
872 | ||
e70ae99e | 873 | if (dot_state == 0) { |
1b6bf175 | 874 | if (check_dot(ctx, dirent, ino, &cd->pctx)) |
3839e657 | 875 | dir_modified++; |
e70ae99e | 876 | } else if (dot_state == 1) { |
28db82a8 TT |
877 | ret = check_dotdot(ctx, dirent, ino, &cd->pctx); |
878 | if (ret < 0) | |
0926668d | 879 | goto abort_free_dict; |
28db82a8 | 880 | if (ret) |
3839e657 TT |
881 | dir_modified++; |
882 | } else if (dirent->inode == ino) { | |
1b6bf175 TT |
883 | problem = PR_2_LINK_DOT; |
884 | if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) { | |
3839e657 TT |
885 | dirent->inode = 0; |
886 | dir_modified++; | |
21c84b71 | 887 | goto next; |
3839e657 TT |
888 | } |
889 | } | |
efc6f628 | 890 | if (!dirent->inode) |
3839e657 | 891 | goto next; |
efc6f628 | 892 | |
3839e657 TT |
893 | /* |
894 | * Make sure the inode listed is a legal one. | |
efc6f628 | 895 | */ |
3839e657 | 896 | if (((dirent->inode != EXT2_ROOT_INO) && |
7f88b043 | 897 | (dirent->inode < EXT2_FIRST_INODE(fs->super))) || |
3839e657 | 898 | (dirent->inode > fs->super->s_inodes_count)) { |
1b6bf175 | 899 | problem = PR_2_BAD_INO; |
1b6bf175 | 900 | } else if (ctx->inode_bb_map && |
c5d2f50d | 901 | (ext2fs_test_inode_bitmap2(ctx->inode_bb_map, |
1b6bf175 TT |
902 | dirent->inode))) { |
903 | /* | |
904 | * If the inode is in a bad block, offer to | |
905 | * clear it. | |
906 | */ | |
907 | problem = PR_2_BB_INODE; | |
e70ae99e | 908 | } else if ((dot_state > 1) && |
b6f79831 | 909 | ((dirent->name_len & 0xFF) == 1) && |
1b6bf175 TT |
910 | (dirent->name[0] == '.')) { |
911 | /* | |
912 | * If there's a '.' entry in anything other | |
913 | * than the first directory entry, it's a | |
914 | * duplicate entry that should be removed. | |
915 | */ | |
916 | problem = PR_2_DUP_DOT; | |
e70ae99e | 917 | } else if ((dot_state > 1) && |
b6f79831 | 918 | ((dirent->name_len & 0xFF) == 2) && |
efc6f628 | 919 | (dirent->name[0] == '.') && |
1b6bf175 TT |
920 | (dirent->name[1] == '.')) { |
921 | /* | |
922 | * If there's a '..' entry in anything other | |
923 | * than the second directory entry, it's a | |
924 | * duplicate entry that should be removed. | |
925 | */ | |
926 | problem = PR_2_DUP_DOT_DOT; | |
e70ae99e | 927 | } else if ((dot_state > 1) && |
1b6bf175 TT |
928 | (dirent->inode == EXT2_ROOT_INO)) { |
929 | /* | |
930 | * Don't allow links to the root directory. | |
931 | * We check this specially to make sure we | |
932 | * catch this error case even if the root | |
933 | * directory hasn't been created yet. | |
934 | */ | |
935 | problem = PR_2_LINK_ROOT; | |
e70ae99e | 936 | } else if ((dot_state > 1) && |
c40db6d5 TT |
937 | (dirent->name_len & 0xFF) == 0) { |
938 | /* | |
939 | * Don't allow zero-length directory names. | |
940 | */ | |
941 | problem = PR_2_NULL_NAME; | |
21c84b71 TT |
942 | } |
943 | ||
1b6bf175 TT |
944 | if (problem) { |
945 | if (fix_problem(ctx, problem, &cd->pctx)) { | |
21c84b71 TT |
946 | dirent->inode = 0; |
947 | dir_modified++; | |
948 | goto next; | |
1b6bf175 TT |
949 | } else { |
950 | ext2fs_unmark_valid(fs); | |
951 | if (problem == PR_2_BAD_INO) | |
952 | goto next; | |
21c84b71 | 953 | } |
3839e657 TT |
954 | } |
955 | ||
956 | /* | |
957 | * If the inode was marked as having bad fields in | |
958 | * pass1, process it and offer to fix/clear it. | |
959 | * (We wait until now so that we can display the | |
960 | * pathname to the user.) | |
961 | */ | |
1b6bf175 | 962 | if (ctx->inode_bad_map && |
c5d2f50d | 963 | ext2fs_test_inode_bitmap2(ctx->inode_bad_map, |
3839e657 | 964 | dirent->inode)) { |
e72a9ba3 | 965 | if (e2fsck_process_bad_inode(ctx, ino, |
bcf9c5d4 TT |
966 | dirent->inode, |
967 | buf + fs->blocksize)) { | |
3839e657 TT |
968 | dirent->inode = 0; |
969 | dir_modified++; | |
970 | goto next; | |
971 | } | |
a02ce9df | 972 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
08b21301 | 973 | return DIRENT_ABORT; |
3839e657 TT |
974 | } |
975 | ||
49a7360b JS |
976 | group = ext2fs_group_of_ino(fs, dirent->inode); |
977 | first_unused_inode = group * fs->super->s_inodes_per_group + | |
978 | 1 + fs->super->s_inodes_per_group - | |
979 | fs->group_desc[group].bg_itable_unused; | |
980 | cd->pctx.group = group; | |
981 | ||
982 | /* | |
42e89ce7 TT |
983 | * Check if the inode was missed out because |
984 | * _INODE_UNINIT flag was set or bg_itable_unused was | |
985 | * incorrect. If so, clear the _INODE_UNINIT flag and | |
986 | * restart e2fsck. In the future it would be nice if | |
987 | * we could call a function in pass1.c that checks the | |
988 | * newly visible inodes. | |
49a7360b JS |
989 | */ |
990 | if (fs->group_desc[group].bg_flags & EXT2_BG_INODE_UNINIT) { | |
6267ee49 | 991 | pctx.num = dirent->inode; |
49a7360b JS |
992 | if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT, |
993 | &cd->pctx)){ | |
994 | fs->group_desc[group].bg_flags &= | |
995 | ~EXT2_BG_INODE_UNINIT; | |
42e89ce7 | 996 | ext2fs_mark_super_dirty(fs); |
6267ee49 | 997 | ctx->flags |= E2F_FLAG_RESTART_LATER; |
49a7360b JS |
998 | } else { |
999 | ext2fs_unmark_valid(fs); | |
1000 | if (problem == PR_2_BAD_INO) | |
1001 | goto next; | |
1002 | } | |
1003 | } else if (dirent->inode >= first_unused_inode) { | |
6267ee49 | 1004 | pctx.num = dirent->inode; |
49a7360b JS |
1005 | if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){ |
1006 | fs->group_desc[group].bg_itable_unused = 0; | |
49a7360b | 1007 | ext2fs_mark_super_dirty(fs); |
6267ee49 | 1008 | ctx->flags |= E2F_FLAG_RESTART_LATER; |
49a7360b JS |
1009 | } else { |
1010 | ext2fs_unmark_valid(fs); | |
1011 | if (problem == PR_2_BAD_INO) | |
1012 | goto next; | |
1013 | } | |
1014 | } | |
1015 | ||
c5d2f50d | 1016 | if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, |
49a7360b JS |
1017 | dirent->inode))) { |
1018 | /* | |
1019 | * If the inode is unused, offer to clear it. | |
1020 | */ | |
1021 | problem = PR_2_UNUSED_INODE; | |
1022 | } | |
1023 | ||
1024 | if (problem) { | |
1025 | if (fix_problem(ctx, problem, &cd->pctx)) { | |
1026 | dirent->inode = 0; | |
1027 | dir_modified++; | |
1028 | goto next; | |
1029 | } else { | |
1030 | ext2fs_unmark_valid(fs); | |
1031 | if (problem == PR_2_BAD_INO) | |
1032 | goto next; | |
1033 | } | |
1034 | } | |
1035 | ||
1b6bf175 TT |
1036 | if (check_name(ctx, dirent, ino, &cd->pctx)) |
1037 | dir_modified++; | |
1038 | ||
aa4115a4 TT |
1039 | if (check_filetype(ctx, dirent, ino, &cd->pctx)) |
1040 | dir_modified++; | |
1041 | ||
8fdc9985 TT |
1042 | #ifdef ENABLE_HTREE |
1043 | if (dx_db) { | |
1044 | ext2fs_dirhash(dx_dir->hashversion, dirent->name, | |
503f9e7f TT |
1045 | (dirent->name_len & 0xFF), |
1046 | fs->super->s_hash_seed, &hash, 0); | |
8fdc9985 TT |
1047 | if (hash < dx_db->min_hash) |
1048 | dx_db->min_hash = hash; | |
1049 | if (hash > dx_db->max_hash) | |
1050 | dx_db->max_hash = hash; | |
1051 | } | |
1052 | #endif | |
1053 | ||
3839e657 TT |
1054 | /* |
1055 | * If this is a directory, then mark its parent in its | |
1056 | * dir_info structure. If the parent field is already | |
1057 | * filled in, then this directory has more than one | |
1058 | * hard link. We assume the first link is correct, | |
1059 | * and ask the user if he/she wants to clear this one. | |
1060 | */ | |
e70ae99e | 1061 | if ((dot_state > 1) && |
c5d2f50d | 1062 | (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, |
3839e657 | 1063 | dirent->inode))) { |
28db82a8 TT |
1064 | if (e2fsck_dir_info_get_parent(ctx, dirent->inode, |
1065 | &subdir_parent)) { | |
1b6bf175 TT |
1066 | cd->pctx.ino = dirent->inode; |
1067 | fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx); | |
0926668d | 1068 | goto abort_free_dict; |
3839e657 | 1069 | } |
28db82a8 TT |
1070 | if (subdir_parent) { |
1071 | cd->pctx.ino2 = subdir_parent; | |
1b6bf175 | 1072 | if (fix_problem(ctx, PR_2_LINK_DIR, |
21c84b71 | 1073 | &cd->pctx)) { |
3839e657 TT |
1074 | dirent->inode = 0; |
1075 | dir_modified++; | |
1076 | goto next; | |
21c84b71 TT |
1077 | } |
1078 | cd->pctx.ino2 = 0; | |
28db82a8 | 1079 | } else { |
efc6f628 | 1080 | (void) e2fsck_dir_info_set_parent(ctx, |
28db82a8 TT |
1081 | dirent->inode, ino); |
1082 | } | |
3839e657 | 1083 | } |
0926668d TT |
1084 | |
1085 | if (dups_found) { | |
1086 | ; | |
1087 | } else if (dict_lookup(&de_dict, dirent)) { | |
1088 | clear_problem_context(&pctx); | |
1089 | pctx.ino = ino; | |
1090 | pctx.dirent = dirent; | |
1091 | fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx); | |
1092 | if (!ctx->dirs_to_hash) | |
1093 | ext2fs_u32_list_create(&ctx->dirs_to_hash, 50); | |
1094 | if (ctx->dirs_to_hash) | |
1095 | ext2fs_u32_list_add(ctx->dirs_to_hash, ino); | |
1096 | dups_found++; | |
1097 | } else | |
1098 | dict_alloc_insert(&de_dict, dirent, dirent); | |
efc6f628 | 1099 | |
1b6bf175 TT |
1100 | ext2fs_icount_increment(ctx->inode_count, dirent->inode, |
1101 | &links); | |
21c84b71 | 1102 | if (links > 1) |
1b6bf175 TT |
1103 | ctx->fs_links_count++; |
1104 | ctx->fs_total_count++; | |
3839e657 | 1105 | next: |
e70ae99e | 1106 | prev = dirent; |
5dd77dbe | 1107 | if (dir_modified) |
8a480350 | 1108 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); |
5dd77dbe | 1109 | offset += rec_len; |
e70ae99e | 1110 | dot_state++; |
3839e657 TT |
1111 | } while (offset < fs->blocksize); |
1112 | #if 0 | |
1113 | printf("\n"); | |
1114 | #endif | |
8fdc9985 TT |
1115 | #ifdef ENABLE_HTREE |
1116 | if (dx_db) { | |
1117 | #ifdef DX_DEBUG | |
1118 | printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n", | |
1119 | db->blockcnt, dx_db->type, | |
1120 | dx_db->min_hash, dx_db->max_hash); | |
1121 | #endif | |
b7a00563 | 1122 | cd->pctx.dir = cd->pctx.ino; |
8fdc9985 TT |
1123 | if ((dx_db->type == DX_DIRBLOCK_ROOT) || |
1124 | (dx_db->type == DX_DIRBLOCK_NODE)) | |
1125 | parse_int_node(fs, db, cd, dx_dir, buf); | |
1126 | } | |
1127 | #endif /* ENABLE_HTREE */ | |
3839e657 | 1128 | if (offset != fs->blocksize) { |
5dd77dbe | 1129 | cd->pctx.num = rec_len - fs->blocksize + offset; |
1b6bf175 TT |
1130 | if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) { |
1131 | dirent->rec_len = cd->pctx.num; | |
1132 | dir_modified++; | |
1133 | } | |
3839e657 TT |
1134 | } |
1135 | if (dir_modified) { | |
1b6bf175 TT |
1136 | cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf); |
1137 | if (cd->pctx.errcode) { | |
08b21301 | 1138 | if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK, |
0926668d TT |
1139 | &cd->pctx)) |
1140 | goto abort_free_dict; | |
3839e657 TT |
1141 | } |
1142 | ext2fs_mark_changed(fs); | |
1143 | } | |
0926668d | 1144 | dict_free_nodes(&de_dict); |
3839e657 | 1145 | return 0; |
0926668d | 1146 | abort_free_dict: |
0926668d | 1147 | ctx->flags |= E2F_FLAG_ABORT; |
49a7360b | 1148 | dict_free_nodes(&de_dict); |
0926668d | 1149 | return DIRENT_ABORT; |
3839e657 TT |
1150 | } |
1151 | ||
1152 | /* | |
1153 | * This function is called to deallocate a block, and is an interator | |
1154 | * functioned called by deallocate inode via ext2fs_iterate_block(). | |
1155 | */ | |
1156 | static int deallocate_inode_block(ext2_filsys fs, | |
133a56dc | 1157 | blk_t *block_nr, |
54434927 TT |
1158 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), |
1159 | blk_t ref_block EXT2FS_ATTR((unused)), | |
1160 | int ref_offset EXT2FS_ATTR((unused)), | |
133a56dc | 1161 | void *priv_data) |
3839e657 | 1162 | { |
54dc7ca2 | 1163 | e2fsck_t ctx = (e2fsck_t) priv_data; |
efc6f628 | 1164 | |
1917875f | 1165 | if (HOLE_BLKADDR(*block_nr)) |
3839e657 | 1166 | return 0; |
1ba7a2f2 TT |
1167 | if ((*block_nr < fs->super->s_first_data_block) || |
1168 | (*block_nr >= fs->super->s_blocks_count)) | |
1169 | return 0; | |
c5d2f50d | 1170 | ext2fs_unmark_block_bitmap2(ctx->block_found_map, *block_nr); |
0684a4f3 | 1171 | ext2fs_block_alloc_stats(fs, *block_nr, -1); |
3839e657 TT |
1172 | return 0; |
1173 | } | |
efc6f628 | 1174 | |
3839e657 TT |
1175 | /* |
1176 | * This fuction deallocates an inode | |
1177 | */ | |
4035f40b | 1178 | static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf) |
3839e657 | 1179 | { |
1b6bf175 | 1180 | ext2_filsys fs = ctx->fs; |
3839e657 | 1181 | struct ext2_inode inode; |
1b6bf175 | 1182 | struct problem_context pctx; |
0684a4f3 | 1183 | __u32 count; |
efc6f628 | 1184 | |
08b21301 | 1185 | e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode"); |
e3df15ab | 1186 | e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode"); |
1b6bf175 TT |
1187 | clear_problem_context(&pctx); |
1188 | pctx.ino = ino; | |
f3db3566 | 1189 | |
3839e657 TT |
1190 | /* |
1191 | * Fix up the bitmaps... | |
1192 | */ | |
f8188fff | 1193 | e2fsck_read_bitmaps(ctx); |
0684a4f3 TT |
1194 | ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode)); |
1195 | ||
1196 | if (inode.i_file_acl && | |
1197 | (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) { | |
1198 | pctx.errcode = ext2fs_adjust_ea_refcount(fs, inode.i_file_acl, | |
1199 | block_buf, -1, &count); | |
1200 | if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) { | |
1201 | pctx.errcode = 0; | |
1202 | count = 1; | |
1203 | } | |
1204 | if (pctx.errcode) { | |
1205 | pctx.blk = inode.i_file_acl; | |
1206 | fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx); | |
1207 | ctx->flags |= E2F_FLAG_ABORT; | |
1208 | return; | |
1209 | } | |
1210 | if (count == 0) { | |
c5d2f50d | 1211 | ext2fs_unmark_block_bitmap2(ctx->block_found_map, |
0684a4f3 TT |
1212 | inode.i_file_acl); |
1213 | ext2fs_block_alloc_stats(fs, inode.i_file_acl, -1); | |
1214 | } | |
1215 | inode.i_file_acl = 0; | |
1216 | } | |
3839e657 | 1217 | |
21c84b71 | 1218 | if (!ext2fs_inode_has_valid_blocks(&inode)) |
3839e657 | 1219 | return; |
a4742691 | 1220 | |
b94a052a | 1221 | if (LINUX_S_ISREG(inode.i_mode) && |
a4742691 TT |
1222 | (inode.i_size_high || inode.i_size & 0x80000000UL)) |
1223 | ctx->large_files--; | |
1224 | ||
133a56dc | 1225 | pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf, |
1b6bf175 TT |
1226 | deallocate_inode_block, ctx); |
1227 | if (pctx.errcode) { | |
1228 | fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx); | |
08b21301 TT |
1229 | ctx->flags |= E2F_FLAG_ABORT; |
1230 | return; | |
1b6bf175 | 1231 | } |
3839e657 TT |
1232 | } |
1233 | ||
8fdc9985 TT |
1234 | /* |
1235 | * This fuction clears the htree flag on an inode | |
1236 | */ | |
1237 | static void clear_htree(e2fsck_t ctx, ext2_ino_t ino) | |
1238 | { | |
1239 | struct ext2_inode inode; | |
efc6f628 | 1240 | |
8fdc9985 TT |
1241 | e2fsck_read_inode(ctx, ino, &inode, "clear_htree"); |
1242 | inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL; | |
1243 | e2fsck_write_inode(ctx, ino, &inode, "clear_htree"); | |
b7a00563 TT |
1244 | if (ctx->dirs_to_hash) |
1245 | ext2fs_u32_list_add(ctx->dirs_to_hash, ino); | |
8fdc9985 TT |
1246 | } |
1247 | ||
1248 | ||
86c627ec | 1249 | extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir, |
bcf9c5d4 | 1250 | ext2_ino_t ino, char *buf) |
3839e657 | 1251 | { |
1b6bf175 | 1252 | ext2_filsys fs = ctx->fs; |
3839e657 | 1253 | struct ext2_inode inode; |
3839e657 | 1254 | int inode_modified = 0; |
6c313fd4 | 1255 | int not_fixed = 0; |
1e3472c5 | 1256 | unsigned char *frag, *fsize; |
21c84b71 | 1257 | struct problem_context pctx; |
08b21301 | 1258 | int problem = 0; |
3839e657 | 1259 | |
08b21301 | 1260 | e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode"); |
21c84b71 TT |
1261 | |
1262 | clear_problem_context(&pctx); | |
1263 | pctx.ino = ino; | |
1264 | pctx.dir = dir; | |
1265 | pctx.inode = &inode; | |
1266 | ||
6c313fd4 | 1267 | if (inode.i_file_acl && |
f76344fb TT |
1268 | !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) { |
1269 | if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) { | |
1270 | inode.i_file_acl = 0; | |
f76344fb TT |
1271 | inode_modified++; |
1272 | } else | |
1273 | not_fixed++; | |
1274 | } | |
6c313fd4 | 1275 | |
50e1e10f TT |
1276 | if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) && |
1277 | !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) && | |
1278 | !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) && | |
08b21301 TT |
1279 | !(LINUX_S_ISSOCK(inode.i_mode))) |
1280 | problem = PR_2_BAD_MODE; | |
fdbdea09 | 1281 | else if (LINUX_S_ISCHR(inode.i_mode) |
0684a4f3 | 1282 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
08b21301 | 1283 | problem = PR_2_BAD_CHAR_DEV; |
fdbdea09 | 1284 | else if (LINUX_S_ISBLK(inode.i_mode) |
0684a4f3 | 1285 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
08b21301 | 1286 | problem = PR_2_BAD_BLOCK_DEV; |
fdbdea09 | 1287 | else if (LINUX_S_ISFIFO(inode.i_mode) |
0684a4f3 | 1288 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
1dde43f0 | 1289 | problem = PR_2_BAD_FIFO; |
fdbdea09 | 1290 | else if (LINUX_S_ISSOCK(inode.i_mode) |
0684a4f3 | 1291 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
1dde43f0 | 1292 | problem = PR_2_BAD_SOCKET; |
fdbdea09 | 1293 | else if (LINUX_S_ISLNK(inode.i_mode) |
7cadc577 | 1294 | && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) { |
bcf9c5d4 | 1295 | problem = PR_2_INVALID_SYMLINK; |
67052a8a | 1296 | } |
1dde43f0 | 1297 | |
08b21301 TT |
1298 | if (problem) { |
1299 | if (fix_problem(ctx, problem, &pctx)) { | |
1b6bf175 | 1300 | deallocate_inode(ctx, ino, 0); |
a02ce9df | 1301 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
08b21301 | 1302 | return 0; |
7cf73dcd | 1303 | return 1; |
6c313fd4 TT |
1304 | } else |
1305 | not_fixed++; | |
08b21301 | 1306 | problem = 0; |
7cf73dcd | 1307 | } |
efc6f628 | 1308 | |
6c313fd4 TT |
1309 | if (inode.i_faddr) { |
1310 | if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) { | |
1311 | inode.i_faddr = 0; | |
1312 | inode_modified++; | |
1313 | } else | |
1314 | not_fixed++; | |
3839e657 | 1315 | } |
1e3472c5 TT |
1316 | |
1317 | switch (fs->super->s_creator_os) { | |
1e3472c5 TT |
1318 | case EXT2_OS_HURD: |
1319 | frag = &inode.osd2.hurd2.h_i_frag; | |
1320 | fsize = &inode.osd2.hurd2.h_i_fsize; | |
1321 | break; | |
1e3472c5 TT |
1322 | default: |
1323 | frag = fsize = 0; | |
1324 | } | |
21c84b71 TT |
1325 | if (frag && *frag) { |
1326 | pctx.num = *frag; | |
1b6bf175 | 1327 | if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) { |
21c84b71 TT |
1328 | *frag = 0; |
1329 | inode_modified++; | |
7e0282c5 TT |
1330 | } else |
1331 | not_fixed++; | |
21c84b71 TT |
1332 | pctx.num = 0; |
1333 | } | |
1334 | if (fsize && *fsize) { | |
1335 | pctx.num = *fsize; | |
1b6bf175 | 1336 | if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) { |
21c84b71 TT |
1337 | *fsize = 0; |
1338 | inode_modified++; | |
6c313fd4 TT |
1339 | } else |
1340 | not_fixed++; | |
21c84b71 TT |
1341 | pctx.num = 0; |
1342 | } | |
1343 | ||
5d17119d | 1344 | if ((fs->super->s_creator_os == EXT2_OS_LINUX) && |
efc6f628 | 1345 | !(fs->super->s_feature_ro_compat & |
5d17119d TT |
1346 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE) && |
1347 | (inode.osd2.linux2.l_i_blocks_hi != 0)) { | |
1348 | pctx.num = inode.osd2.linux2.l_i_blocks_hi; | |
1349 | if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) { | |
1350 | inode.osd2.linux2.l_i_blocks_hi = 0; | |
1351 | inode_modified++; | |
1352 | } | |
1353 | } | |
1354 | ||
911ec626 TT |
1355 | if (!(fs->super->s_feature_incompat & |
1356 | EXT4_FEATURE_INCOMPAT_64BIT) && | |
1357 | inode.osd2.linux2.l_i_file_acl_high != 0) { | |
1358 | pctx.num = inode.osd2.linux2.l_i_file_acl_high; | |
1359 | if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) { | |
1360 | inode.osd2.linux2.l_i_file_acl_high = 0; | |
1361 | inode_modified++; | |
1362 | } else | |
1363 | not_fixed++; | |
1364 | } | |
1365 | ||
342d847d TT |
1366 | if (inode.i_file_acl && |
1367 | ((inode.i_file_acl < fs->super->s_first_data_block) || | |
6c313fd4 TT |
1368 | (inode.i_file_acl >= fs->super->s_blocks_count))) { |
1369 | if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) { | |
1370 | inode.i_file_acl = 0; | |
1371 | inode_modified++; | |
1372 | } else | |
1373 | not_fixed++; | |
342d847d | 1374 | } |
21c84b71 | 1375 | if (inode.i_dir_acl && |
6c313fd4 TT |
1376 | LINUX_S_ISDIR(inode.i_mode)) { |
1377 | if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) { | |
1378 | inode.i_dir_acl = 0; | |
1379 | inode_modified++; | |
1380 | } else | |
1381 | not_fixed++; | |
21c84b71 | 1382 | } |
6c313fd4 | 1383 | |
f3db3566 | 1384 | if (inode_modified) |
08b21301 | 1385 | e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode"); |
f76344fb | 1386 | if (!not_fixed && ctx->inode_bad_map) |
c5d2f50d | 1387 | ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino); |
3839e657 TT |
1388 | return 0; |
1389 | } | |
1390 | ||
50e1e10f TT |
1391 | |
1392 | /* | |
1393 | * allocate_dir_block --- this function allocates a new directory | |
1394 | * block for a particular inode; this is done if a directory has | |
1395 | * a "hole" in it, or if a directory has a illegal block number | |
1396 | * that was zeroed out and now needs to be replaced. | |
1397 | */ | |
1b6bf175 | 1398 | static int allocate_dir_block(e2fsck_t ctx, |
21c84b71 | 1399 | struct ext2_db_entry *db, |
efc6f628 | 1400 | char *buf EXT2FS_ATTR((unused)), |
54434927 | 1401 | struct problem_context *pctx) |
50e1e10f | 1402 | { |
1b6bf175 | 1403 | ext2_filsys fs = ctx->fs; |
c5d2f50d | 1404 | blk64_t blk; |
50e1e10f TT |
1405 | char *block; |
1406 | struct ext2_inode inode; | |
50e1e10f | 1407 | |
1b6bf175 | 1408 | if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0) |
50e1e10f TT |
1409 | return 1; |
1410 | ||
1411 | /* | |
1412 | * Read the inode and block bitmaps in; we'll be messing with | |
1413 | * them. | |
1414 | */ | |
f8188fff | 1415 | e2fsck_read_bitmaps(ctx); |
efc6f628 | 1416 | |
50e1e10f TT |
1417 | /* |
1418 | * First, find a free block | |
1419 | */ | |
c5d2f50d | 1420 | pctx->errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk); |
1b6bf175 TT |
1421 | if (pctx->errcode) { |
1422 | pctx->str = "ext2fs_new_block"; | |
1423 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
50e1e10f TT |
1424 | return 1; |
1425 | } | |
c5d2f50d VAH |
1426 | ext2fs_mark_block_bitmap2(ctx->block_found_map, blk); |
1427 | ext2fs_mark_block_bitmap2(fs->block_map, blk); | |
50e1e10f TT |
1428 | ext2fs_mark_bb_dirty(fs); |
1429 | ||
1430 | /* | |
1431 | * Now let's create the actual data block for the inode | |
1432 | */ | |
1433 | if (db->blockcnt) | |
1b6bf175 | 1434 | pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block); |
50e1e10f | 1435 | else |
1b6bf175 TT |
1436 | pctx->errcode = ext2fs_new_dir_block(fs, db->ino, |
1437 | EXT2_ROOT_INO, &block); | |
50e1e10f | 1438 | |
1b6bf175 TT |
1439 | if (pctx->errcode) { |
1440 | pctx->str = "ext2fs_new_dir_block"; | |
1441 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
50e1e10f TT |
1442 | return 1; |
1443 | } | |
1444 | ||
1b6bf175 | 1445 | pctx->errcode = ext2fs_write_dir_block(fs, blk, block); |
c4e3d3f3 | 1446 | ext2fs_free_mem(&block); |
1b6bf175 TT |
1447 | if (pctx->errcode) { |
1448 | pctx->str = "ext2fs_write_dir_block"; | |
1449 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
50e1e10f TT |
1450 | return 1; |
1451 | } | |
1452 | ||
1453 | /* | |
1454 | * Update the inode block count | |
1455 | */ | |
08b21301 | 1456 | e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block"); |
1ca1059f | 1457 | ext2fs_iblk_add_blocks(fs, &inode, 1); |
50e1e10f TT |
1458 | if (inode.i_size < (db->blockcnt+1) * fs->blocksize) |
1459 | inode.i_size = (db->blockcnt+1) * fs->blocksize; | |
08b21301 | 1460 | e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block"); |
50e1e10f TT |
1461 | |
1462 | /* | |
1463 | * Finally, update the block pointers for the inode | |
1464 | */ | |
1465 | db->blk = blk; | |
133a56dc | 1466 | pctx->errcode = ext2fs_block_iterate2(fs, db->ino, BLOCK_FLAG_HOLE, |
50e1e10f | 1467 | 0, update_dir_block, db); |
1b6bf175 TT |
1468 | if (pctx->errcode) { |
1469 | pctx->str = "ext2fs_block_iterate"; | |
1470 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); | |
50e1e10f TT |
1471 | return 1; |
1472 | } | |
1473 | ||
1474 | return 0; | |
1475 | } | |
1476 | ||
1477 | /* | |
1478 | * This is a helper function for allocate_dir_block(). | |
1479 | */ | |
54434927 | 1480 | static int update_dir_block(ext2_filsys fs EXT2FS_ATTR((unused)), |
50e1e10f | 1481 | blk_t *block_nr, |
133a56dc | 1482 | e2_blkcnt_t blockcnt, |
54434927 | 1483 | blk_t ref_block EXT2FS_ATTR((unused)), |
efc6f628 | 1484 | int ref_offset EXT2FS_ATTR((unused)), |
54dc7ca2 | 1485 | void *priv_data) |
50e1e10f | 1486 | { |
54dc7ca2 | 1487 | struct ext2_db_entry *db; |
50e1e10f | 1488 | |
54dc7ca2 | 1489 | db = (struct ext2_db_entry *) priv_data; |
133a56dc | 1490 | if (db->blockcnt == (int) blockcnt) { |
50e1e10f TT |
1491 | *block_nr = db->blk; |
1492 | return BLOCK_CHANGED; | |
1493 | } | |
1494 | return 0; | |
1495 | } |