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