2 * pass2.c --- check directory structure
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o
7 * This file may be redistributed under the terms of the GNU Public
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:
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
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 '..'.
27 * To minimize disk seek time, the directory blocks are processed in
28 * sorted order of block numbers.
30 * Pass 2 also collects the following information:
31 * - The inode numbers of the subdirectories for each directory.
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
39 * Pass 2 frees the following data structures
40 * - The inode_bad_map bitmap
41 * - The inode_reg_map bitmap
44 #define _GNU_SOURCE 1 /* get strnlen() */
51 #ifdef NO_INLINE_FUNCS
54 #define _INLINE_ inline
57 /* #define DX_DEBUG */
60 * Keeps track of how many times an inode is referenced.
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,
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,
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);
80 struct check_dir_struct {
82 struct problem_context pctx;
87 void e2fsck_pass2(e2fsck_t ctx)
89 struct ext2_super_block *sb = ctx->fs->super;
90 struct problem_context pctx;
91 ext2_filsys fs = ctx->fs;
94 struct resource_track rtrack;
97 struct check_dir_struct cd;
98 struct dx_dir_info *dx_dir;
99 struct dx_dirblock_info *dx_db, *dx_parent;
105 #ifdef RESOURCE_TRACK
106 init_resource_track(&rtrack);
109 clear_problem_context(&cd.pctx);
112 mtrace_print("Pass 2");
115 if (!(ctx->options & E2F_OPT_PREEN))
116 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
118 cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
119 0, ctx->inode_link_info,
121 if (cd.pctx.errcode) {
122 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
123 ctx->flags |= E2F_FLAG_ABORT;
126 buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
127 "directory scan buffer");
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.)
134 dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO);
136 dir->parent = EXT2_ROOT_INO;
141 cd.max = ext2fs_dblist_count(fs->dblist);
144 (void) (ctx->progress)(ctx, 2, 0, cd.max);
146 if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX)
147 ext2fs_dblist_sort(fs->dblist, special_dir_block_cmp);
149 cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
151 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
153 if (cd.pctx.errcode) {
154 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
155 ctx->flags |= E2F_FLAG_ABORT;
160 for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
161 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
163 if (dx_dir->numblocks == 0)
165 clear_problem_context(&pctx);
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;
172 dx_db->flags |= DX_FLAG_REFERENCED;
174 * Find all of the first and last leaf blocks, and
175 * update their parent's min and max hash values
177 for (b=0, dx_db = dx_dir->dx_block;
178 b < dx_dir->numblocks;
180 if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
181 !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
183 dx_parent = &dx_dir->dx_block[dx_db->parent];
185 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
187 if (dx_db->flags & DX_FLAG_FIRST)
188 dx_parent->min_hash = dx_db->min_hash;
190 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
192 if (dx_db->flags & DX_FLAG_LAST)
193 dx_parent->max_hash = dx_db->max_hash;
196 for (b=0, dx_db = dx_dir->dx_block;
197 b < dx_dir->numblocks;
200 pctx.group = dx_db->parent;
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);
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);
219 * This test doesn't apply for the root block
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);
230 if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
231 code = PR_2_HTREE_NOTREF;
232 fix_problem(ctx, code, &pctx);
234 } else if (dx_db->flags & DX_FLAG_DUP_REF) {
235 code = PR_2_HTREE_DUPREF;
236 fix_problem(ctx, code, &pctx);
242 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
243 clear_htree(ctx, dx_dir->ino);
244 dx_dir->numblocks = 0;
246 #ifdef ENABLE_HTREE_CLEAR
247 if (dx_dir->numblocks) {
248 fix_problem(ctx, PR_2_HTREE_FCLR, &pctx);
249 clear_htree(ctx, dx_dir->ino);
250 dx_dir->numblocks = 0;
255 ext2fs_free_mem(&buf);
256 ext2fs_free_dblist(fs->dblist);
258 if (ctx->inode_bad_map) {
259 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
260 ctx->inode_bad_map = 0;
262 if (ctx->inode_reg_map) {
263 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
264 ctx->inode_reg_map = 0;
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);
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);
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);
291 #ifdef RESOURCE_TRACK
292 if (ctx->options & E2F_OPT_TIME2) {
293 e2fsck_clear_progbar(ctx);
294 print_resource_track(_("Pass 2"), &rtrack);
299 #define MAX_DEPTH 32000
300 static int htree_depth(struct dx_dir_info *dx_dir,
301 struct dx_dirblock_info *dx_db)
305 while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
306 dx_db = &dx_dir->dx_block[dx_db->parent];
312 static int dict_de_cmp(const void *a, const void *b)
314 const struct ext2_dir_entry *de_a, *de_b;
317 de_a = (const struct ext2_dir_entry *) a;
318 a_len = de_a->name_len & 0xFF;
319 de_b = (const struct ext2_dir_entry *) b;
320 b_len = de_b->name_len & 0xFF;
323 return (a_len - b_len);
325 return strncmp(de_a->name, de_b->name, a_len);
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.
334 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
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;
341 if (db_a->blockcnt && !db_b->blockcnt)
344 if (!db_a->blockcnt && db_b->blockcnt)
347 if (db_a->blk != db_b->blk)
348 return (int) (db_a->blk - db_b->blk);
350 if (db_a->ino != db_b->ino)
351 return (int) (db_a->ino - db_b->ino);
353 return (int) (db_a->blockcnt - db_b->blockcnt);
358 * Make sure the first entry in the directory is '.', and that the
359 * directory entry is sane.
361 static int check_dot(e2fsck_t ctx,
362 struct ext2_dir_entry *dirent,
363 ext2_ino_t ino, struct problem_context *pctx)
365 struct ext2_dir_entry *nextdir;
372 problem = PR_2_MISSING_DOT;
373 else if (((dirent->name_len & 0xFF) != 1) ||
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;
380 if (fix_problem(ctx, problem, pctx)) {
381 if (dirent->rec_len < 12)
382 dirent->rec_len = 12;
384 dirent->name_len = 1;
385 dirent->name[0] = '.';
386 dirent->name[1] = '\0';
391 if (dirent->inode != ino) {
392 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
397 if (dirent->rec_len > 12) {
398 new_len = dirent->rec_len - 12;
401 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
402 nextdir = (struct ext2_dir_entry *)
403 ((char *) dirent + 12);
404 dirent->rec_len = 12;
405 nextdir->rec_len = new_len;
407 nextdir->name_len = 0;
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.
420 static int check_dotdot(e2fsck_t ctx,
421 struct ext2_dir_entry *dirent,
422 struct dir_info *dir, struct problem_context *pctx)
427 problem = PR_2_MISSING_DOT_DOT;
428 else if (((dirent->name_len & 0xFF) != 2) ||
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;
436 if (fix_problem(ctx, problem, pctx)) {
437 if (dirent->rec_len < 12)
438 dirent->rec_len = 12;
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.
444 dirent->inode = EXT2_ROOT_INO;
445 dirent->name_len = 2;
446 dirent->name[0] = '.';
447 dirent->name[1] = '.';
448 dirent->name[2] = '\0';
453 dir->dotdot = dirent->inode;
458 * Check to make sure a directory entry doesn't contain any illegal
461 static int check_name(e2fsck_t ctx,
462 struct ext2_dir_entry *dirent,
463 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
464 struct problem_context *pctx)
470 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
471 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
473 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
476 dirent->name[i] = '.';
485 * Check the directory filetype (if present)
487 static _INLINE_ int check_filetype(e2fsck_t ctx,
488 struct ext2_dir_entry *dirent,
489 ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
490 struct problem_context *pctx)
492 int filetype = dirent->name_len >> 8;
493 int should_be = EXT2_FT_UNKNOWN;
494 struct ext2_inode inode;
496 if (!(ctx->fs->super->s_feature_incompat &
497 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
499 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
501 dirent->name_len = dirent->name_len & 0xFF;
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,
509 should_be = EXT2_FT_REG_FILE;
510 } else if (ctx->inode_bad_map &&
511 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
515 e2fsck_read_inode(ctx, dirent->inode, &inode,
517 should_be = ext2_file_type(inode.i_mode);
519 if (filetype == should_be)
521 pctx->num = should_be;
523 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
527 dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8;
532 static 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,
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;
542 int i, expect_limit, count;
544 ext2_dirhash_t min_hash = 0xffffffff;
545 ext2_dirhash_t max_hash = 0;
546 ext2_dirhash_t hash = 0, prev_hash;
548 if (db->blockcnt == 0) {
549 root = (struct ext2_dx_root_info *) (block_buf + 24);
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);
560 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
562 ent = (struct ext2_dx_entry *) (block_buf+8);
564 limit = (struct ext2_dx_countlimit *) ent;
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));
573 count = ext2fs_le16_to_cpu(limit->count);
574 expect_limit = (fs->blocksize - ((char *) ent - block_buf)) /
575 sizeof(struct ext2_dx_entry);
576 if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
577 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
578 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
581 if (count > expect_limit) {
582 cd->pctx.num = count;
583 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
585 count = expect_limit;
588 for (i=0; i < count; i++) {
590 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
592 printf("Entry #%d: Hash 0x%08x, block %d\n", i,
593 hash, ext2fs_le32_to_cpu(ent[i].block));
595 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
596 /* Check to make sure the block is valid */
597 if (blk > (blk_t) dx_dir->numblocks) {
599 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
603 if (hash < prev_hash &&
604 fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
606 dx_db = &dx_dir->dx_block[blk];
607 if (dx_db->flags & DX_FLAG_REFERENCED) {
608 dx_db->flags |= DX_FLAG_DUP_REF;
610 dx_db->flags |= DX_FLAG_REFERENCED;
611 dx_db->parent = db->blockcnt;
617 dx_db->node_min_hash = hash;
619 dx_db->node_max_hash =
620 ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
622 dx_db->node_max_hash = 0xfffffffe;
623 dx_db->flags |= DX_FLAG_LAST;
626 dx_db->flags |= DX_FLAG_FIRST;
629 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
630 db->blockcnt, min_hash, max_hash);
632 dx_db = &dx_dir->dx_block[db->blockcnt];
633 dx_db->min_hash = min_hash;
634 dx_db->max_hash = max_hash;
638 clear_htree(cd->ctx, cd->pctx.ino);
639 dx_dir->numblocks = 0;
641 #endif /* ENABLE_HTREE */
644 * Given a busted directory, try to salvage it somehow.
647 static void salvage_directory(ext2_filsys fs,
648 struct ext2_dir_entry *dirent,
649 struct ext2_dir_entry *prev,
650 unsigned int *offset)
652 char *cp = (char *) dirent;
653 int left = fs->blocksize - *offset - dirent->rec_len;
654 int name_len = dirent->name_len & 0xFF;
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.
660 if ((left >= 12) && (dirent->rec_len == 8)) {
661 memmove(cp, cp+8, left);
662 memset(cp + left, 0, 8);
666 * If the directory entry overruns the end of the directory
667 * block, and the name is small enough to fit, then adjust the
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;
678 * If the directory entry is a multiple of four, so it is
679 * valid, let the previous directory entry absorb the invalid
682 if (prev && dirent->rec_len && (dirent->rec_len % 4) == 0) {
683 prev->rec_len += dirent->rec_len;
684 *offset += dirent->rec_len;
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.
694 prev->rec_len += fs->blocksize - *offset;
695 *offset = fs->blocksize;
697 dirent->rec_len = fs->blocksize - *offset;
698 dirent->name_len = 0;
703 static int check_dir_block(ext2_filsys fs,
704 struct ext2_db_entry *db,
707 struct dir_info *subdir, *dir;
708 struct dx_dir_info *dx_dir;
710 struct dx_dirblock_info *dx_db = 0;
711 #endif /* ENABLE_HTREE */
712 struct ext2_dir_entry *dirent, *prev;
714 unsigned int offset = 0;
715 int dir_modified = 0;
717 blk_t block_nr = db->blk;
718 ext2_ino_t ino = db->ino;
720 struct check_dir_struct *cd;
724 struct ext2_dx_root_info *root;
725 struct ext2_dx_countlimit *limit;
726 static dict_t de_dict;
727 struct problem_context pctx;
730 cd = (struct check_dir_struct *) priv_data;
734 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
737 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
741 * Make sure the inode is still in use (could have been
742 * deleted in the duplicate/bad blocks pass.
744 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino)))
748 cd->pctx.blk = block_nr;
749 cd->pctx.blkcount = db->blockcnt;
755 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
765 if (ctx->dirs_to_hash &&
766 ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
770 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
774 cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
775 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
776 cd->pctx.errcode = 0; /* We'll handle this ourselves */
777 if (cd->pctx.errcode) {
778 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
779 ctx->flags |= E2F_FLAG_ABORT;
782 memset(buf, 0, fs->blocksize);
785 dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
786 if (dx_dir && dx_dir->numblocks) {
787 if (db->blockcnt >= dx_dir->numblocks) {
788 printf("XXX should never happen!!!\n");
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;
797 dirent = (struct ext2_dir_entry *) buf;
798 limit = (struct ext2_dx_countlimit *) (buf+8);
799 if (db->blockcnt == 0) {
800 root = (struct ext2_dx_root_info *) (buf + 24);
801 dx_db->type = DX_DIRBLOCK_ROOT;
802 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
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;
811 dx_dir->hashversion = root->hash_version;
812 dx_dir->depth = root->indirect_levels + 1;
813 } else if ((dirent->inode == 0) &&
814 (dirent->rec_len == fs->blocksize) &&
815 (dirent->name_len == 0) &&
816 (ext2fs_le16_to_cpu(limit->limit) ==
818 sizeof(struct ext2_dx_entry))))
819 dx_db->type = DX_DIRBLOCK_NODE;
821 #endif /* ENABLE_HTREE */
823 dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
827 dirent = (struct ext2_dir_entry *) (buf + offset);
828 cd->pctx.dirent = dirent;
829 cd->pctx.num = offset;
830 if (((offset + dirent->rec_len) > fs->blocksize) ||
831 (dirent->rec_len < 12) ||
832 ((dirent->rec_len % 4) != 0) ||
833 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
834 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
835 salvage_directory(fs, dirent, prev, &offset);
839 goto abort_free_dict;
841 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
842 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
843 dirent->name_len = EXT2_NAME_LEN;
848 if (dot_state == 0) {
849 if (check_dot(ctx, dirent, ino, &cd->pctx))
851 } else if (dot_state == 1) {
852 dir = e2fsck_get_dir_info(ctx, ino);
854 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
855 goto abort_free_dict;
857 if (check_dotdot(ctx, dirent, dir, &cd->pctx))
859 } else if (dirent->inode == ino) {
860 problem = PR_2_LINK_DOT;
861 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
871 * Make sure the inode listed is a legal one.
873 if (((dirent->inode != EXT2_ROOT_INO) &&
874 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
875 (dirent->inode > fs->super->s_inodes_count)) {
876 problem = PR_2_BAD_INO;
877 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
880 * If the inode is unused, offer to clear it.
882 problem = PR_2_UNUSED_INODE;
883 } else if (ctx->inode_bb_map &&
884 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
887 * If the inode is in a bad block, offer to
890 problem = PR_2_BB_INODE;
891 } else if ((dot_state > 1) &&
892 ((dirent->name_len & 0xFF) == 1) &&
893 (dirent->name[0] == '.')) {
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.
899 problem = PR_2_DUP_DOT;
900 } else if ((dot_state > 1) &&
901 ((dirent->name_len & 0xFF) == 2) &&
902 (dirent->name[0] == '.') &&
903 (dirent->name[1] == '.')) {
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.
909 problem = PR_2_DUP_DOT_DOT;
910 } else if ((dot_state > 1) &&
911 (dirent->inode == EXT2_ROOT_INO)) {
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.
918 problem = PR_2_LINK_ROOT;
919 } else if ((dot_state > 1) &&
920 (dirent->name_len & 0xFF) == 0) {
922 * Don't allow zero-length directory names.
924 problem = PR_2_NULL_NAME;
928 if (fix_problem(ctx, problem, &cd->pctx)) {
933 ext2fs_unmark_valid(fs);
934 if (problem == PR_2_BAD_INO)
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.)
945 if (ctx->inode_bad_map &&
946 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
948 if (e2fsck_process_bad_inode(ctx, ino,
950 buf + fs->blocksize)) {
955 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
959 if (check_name(ctx, dirent, ino, &cd->pctx))
962 if (check_filetype(ctx, dirent, ino, &cd->pctx))
967 ext2fs_dirhash(dx_dir->hashversion, dirent->name,
968 (dirent->name_len & 0xFF),
969 fs->super->s_hash_seed, &hash, 0);
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;
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.
984 if ((dot_state > 1) &&
985 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
987 subdir = e2fsck_get_dir_info(ctx, dirent->inode);
989 cd->pctx.ino = dirent->inode;
990 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
991 goto abort_free_dict;
993 if (subdir->parent) {
994 cd->pctx.ino2 = subdir->parent;
995 if (fix_problem(ctx, PR_2_LINK_DIR,
1003 subdir->parent = ino;
1008 } else if (dict_lookup(&de_dict, dirent)) {
1009 clear_problem_context(&pctx);
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);
1019 dict_alloc_insert(&de_dict, dirent, dirent);
1021 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1024 ctx->fs_links_count++;
1025 ctx->fs_total_count++;
1028 offset += dirent->rec_len;
1030 } while (offset < fs->blocksize);
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);
1041 cd->pctx.dir = cd->pctx.ino;
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);
1046 #endif /* ENABLE_HTREE */
1047 if (offset != fs->blocksize) {
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;
1055 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
1056 if (cd->pctx.errcode) {
1057 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
1059 goto abort_free_dict;
1061 ext2fs_mark_changed(fs);
1063 dict_free_nodes(&de_dict);
1066 dict_free_nodes(&de_dict);
1067 ctx->flags |= E2F_FLAG_ABORT;
1068 return DIRENT_ABORT;
1072 * This function is called to deallocate a block, and is an interator
1073 * functioned called by deallocate inode via ext2fs_iterate_block().
1075 static int deallocate_inode_block(ext2_filsys fs,
1077 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1078 blk_t ref_block EXT2FS_ATTR((unused)),
1079 int ref_offset EXT2FS_ATTR((unused)),
1082 e2fsck_t ctx = (e2fsck_t) priv_data;
1084 if (HOLE_BLKADDR(*block_nr))
1086 if ((*block_nr < fs->super->s_first_data_block) ||
1087 (*block_nr >= fs->super->s_blocks_count))
1089 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
1090 ext2fs_block_alloc_stats(fs, *block_nr, -1);
1095 * This fuction deallocates an inode
1097 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
1099 ext2_filsys fs = ctx->fs;
1100 struct ext2_inode inode;
1101 struct problem_context pctx;
1104 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1105 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1106 inode.i_links_count = 0;
1107 inode.i_dtime = time(0);
1108 e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
1109 clear_problem_context(&pctx);
1113 * Fix up the bitmaps...
1115 e2fsck_read_bitmaps(ctx);
1116 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1117 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1118 if (ctx->inode_bad_map)
1119 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
1120 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1122 if (inode.i_file_acl &&
1123 (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1124 pctx.errcode = ext2fs_adjust_ea_refcount(fs, inode.i_file_acl,
1125 block_buf, -1, &count);
1126 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1131 pctx.blk = inode.i_file_acl;
1132 fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1133 ctx->flags |= E2F_FLAG_ABORT;
1137 ext2fs_unmark_block_bitmap(ctx->block_found_map,
1139 ext2fs_block_alloc_stats(fs, inode.i_file_acl, -1);
1141 inode.i_file_acl = 0;
1144 if (!ext2fs_inode_has_valid_blocks(&inode))
1147 if (LINUX_S_ISREG(inode.i_mode) &&
1148 (inode.i_size_high || inode.i_size & 0x80000000UL))
1151 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
1152 deallocate_inode_block, ctx);
1154 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
1155 ctx->flags |= E2F_FLAG_ABORT;
1161 * This fuction clears the htree flag on an inode
1163 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1165 struct ext2_inode inode;
1167 e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1168 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1169 e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
1170 if (ctx->dirs_to_hash)
1171 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1175 extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1176 ext2_ino_t ino, char *buf)
1178 ext2_filsys fs = ctx->fs;
1179 struct ext2_inode inode;
1180 int inode_modified = 0;
1181 unsigned char *frag, *fsize;
1182 struct problem_context pctx;
1185 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
1187 clear_problem_context(&pctx);
1190 pctx.inode = &inode;
1192 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1193 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1194 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
1195 !(LINUX_S_ISSOCK(inode.i_mode)))
1196 problem = PR_2_BAD_MODE;
1197 else if (LINUX_S_ISCHR(inode.i_mode)
1198 && !e2fsck_pass1_check_device_inode(fs, &inode))
1199 problem = PR_2_BAD_CHAR_DEV;
1200 else if (LINUX_S_ISBLK(inode.i_mode)
1201 && !e2fsck_pass1_check_device_inode(fs, &inode))
1202 problem = PR_2_BAD_BLOCK_DEV;
1203 else if (LINUX_S_ISFIFO(inode.i_mode)
1204 && !e2fsck_pass1_check_device_inode(fs, &inode))
1205 problem = PR_2_BAD_FIFO;
1206 else if (LINUX_S_ISSOCK(inode.i_mode)
1207 && !e2fsck_pass1_check_device_inode(fs, &inode))
1208 problem = PR_2_BAD_SOCKET;
1209 else if (LINUX_S_ISLNK(inode.i_mode)
1210 && !e2fsck_pass1_check_symlink(fs, &inode, buf)) {
1211 problem = PR_2_INVALID_SYMLINK;
1215 if (fix_problem(ctx, problem, &pctx)) {
1216 deallocate_inode(ctx, ino, 0);
1217 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1224 if (inode.i_faddr &&
1225 fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1230 switch (fs->super->s_creator_os) {
1232 frag = &inode.osd2.linux2.l_i_frag;
1233 fsize = &inode.osd2.linux2.l_i_fsize;
1236 frag = &inode.osd2.hurd2.h_i_frag;
1237 fsize = &inode.osd2.hurd2.h_i_fsize;
1240 frag = &inode.osd2.masix2.m_i_frag;
1241 fsize = &inode.osd2.masix2.m_i_fsize;
1246 if (frag && *frag) {
1248 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
1254 if (fsize && *fsize) {
1256 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
1263 if (inode.i_file_acl &&
1264 !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) &&
1265 fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
1266 inode.i_file_acl = 0;
1269 if (inode.i_file_acl &&
1270 ((inode.i_file_acl < fs->super->s_first_data_block) ||
1271 (inode.i_file_acl >= fs->super->s_blocks_count)) &&
1272 fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
1273 inode.i_file_acl = 0;
1276 if (inode.i_dir_acl &&
1277 LINUX_S_ISDIR(inode.i_mode) &&
1278 fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1279 inode.i_dir_acl = 0;
1283 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
1289 * allocate_dir_block --- this function allocates a new directory
1290 * block for a particular inode; this is done if a directory has
1291 * a "hole" in it, or if a directory has a illegal block number
1292 * that was zeroed out and now needs to be replaced.
1294 static int allocate_dir_block(e2fsck_t ctx,
1295 struct ext2_db_entry *db,
1296 char *buf EXT2FS_ATTR((unused)),
1297 struct problem_context *pctx)
1299 ext2_filsys fs = ctx->fs;
1302 struct ext2_inode inode;
1304 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
1308 * Read the inode and block bitmaps in; we'll be messing with
1311 e2fsck_read_bitmaps(ctx);
1314 * First, find a free block
1316 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
1317 if (pctx->errcode) {
1318 pctx->str = "ext2fs_new_block";
1319 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1322 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1323 ext2fs_mark_block_bitmap(fs->block_map, blk);
1324 ext2fs_mark_bb_dirty(fs);
1327 * Now let's create the actual data block for the inode
1330 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
1332 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1333 EXT2_ROOT_INO, &block);
1335 if (pctx->errcode) {
1336 pctx->str = "ext2fs_new_dir_block";
1337 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1341 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
1342 ext2fs_free_mem(&block);
1343 if (pctx->errcode) {
1344 pctx->str = "ext2fs_write_dir_block";
1345 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1350 * Update the inode block count
1352 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1353 inode.i_blocks += fs->blocksize / 512;
1354 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
1355 inode.i_size = (db->blockcnt+1) * fs->blocksize;
1356 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
1359 * Finally, update the block pointers for the inode
1362 pctx->errcode = ext2fs_block_iterate2(fs, db->ino, BLOCK_FLAG_HOLE,
1363 0, update_dir_block, db);
1364 if (pctx->errcode) {
1365 pctx->str = "ext2fs_block_iterate";
1366 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1374 * This is a helper function for allocate_dir_block().
1376 static int update_dir_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1378 e2_blkcnt_t blockcnt,
1379 blk_t ref_block EXT2FS_ATTR((unused)),
1380 int ref_offset EXT2FS_ATTR((unused)),
1383 struct ext2_db_entry *db;
1385 db = (struct ext2_db_entry *) priv_data;
1386 if (db->blockcnt == (int) blockcnt) {
1387 *block_nr = db->blk;
1388 return BLOCK_CHANGED;