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