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
47 #ifdef NO_INLINE_FUNCS
50 #define _INLINE_ inline
54 * Keeps track of how many times an inode is referenced.
56 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf);
57 static int check_dir_block(ext2_filsys fs,
58 struct ext2_db_entry *dir_blocks_info,
60 static int allocate_dir_block(e2fsck_t ctx,
61 struct ext2_db_entry *dir_blocks_info,
62 char *buf, struct problem_context *pctx);
63 static int update_dir_block(ext2_filsys fs,
70 struct check_dir_struct {
72 struct problem_context pctx;
77 void e2fsck_pass2(e2fsck_t ctx)
79 ext2_filsys fs = ctx->fs;
82 struct resource_track rtrack;
85 struct check_dir_struct cd;
88 init_resource_track(&rtrack);
91 clear_problem_context(&cd.pctx);
94 mtrace_print("Pass 2");
97 if (!(ctx->options & E2F_OPT_PREEN))
98 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
100 cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
101 0, ctx->inode_link_info,
103 if (cd.pctx.errcode) {
104 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
105 ctx->flags |= E2F_FLAG_ABORT;
108 buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
109 "directory scan buffer");
112 * Set up the parent pointer for the root directory, if
113 * present. (If the root directory is not present, we will
114 * create it in pass 3.)
116 dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO);
118 dir->parent = EXT2_ROOT_INO;
123 cd.max = ext2fs_dblist_count(fs->dblist);
126 (void) (ctx->progress)(ctx, 2, 0, cd.max);
128 cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
130 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
132 if (cd.pctx.errcode) {
133 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
134 ctx->flags |= E2F_FLAG_ABORT;
138 ext2fs_free_mem((void **) &buf);
139 ext2fs_free_dblist(fs->dblist);
141 if (ctx->inode_bad_map) {
142 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
143 ctx->inode_bad_map = 0;
145 if (ctx->inode_reg_map) {
146 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
147 ctx->inode_reg_map = 0;
149 #ifdef RESOURCE_TRACK
150 if (ctx->options & E2F_OPT_TIME2) {
151 e2fsck_clear_progbar(ctx);
152 print_resource_track("Pass 2", &rtrack);
158 * Make sure the first entry in the directory is '.', and that the
159 * directory entry is sane.
161 static int check_dot(e2fsck_t ctx,
162 struct ext2_dir_entry *dirent,
163 ext2_ino_t ino, struct problem_context *pctx)
165 struct ext2_dir_entry *nextdir;
172 problem = PR_2_MISSING_DOT;
173 else if (((dirent->name_len & 0xFF) != 1) ||
174 (dirent->name[0] != '.'))
175 problem = PR_2_1ST_NOT_DOT;
176 else if (dirent->name[1] != '\0')
177 problem = PR_2_DOT_NULL_TERM;
180 if (fix_problem(ctx, problem, pctx)) {
181 if (dirent->rec_len < 12)
182 dirent->rec_len = 12;
184 dirent->name_len = 1;
185 dirent->name[0] = '.';
186 dirent->name[1] = '\0';
191 if (dirent->inode != ino) {
192 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
197 if (dirent->rec_len > 12) {
198 new_len = dirent->rec_len - 12;
201 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
202 nextdir = (struct ext2_dir_entry *)
203 ((char *) dirent + 12);
204 dirent->rec_len = 12;
205 nextdir->rec_len = new_len;
207 nextdir->name_len = 0;
216 * Make sure the second entry in the directory is '..', and that the
217 * directory entry is sane. We do not check the inode number of '..'
218 * here; this gets done in pass 3.
220 static int check_dotdot(e2fsck_t ctx,
221 struct ext2_dir_entry *dirent,
222 struct dir_info *dir, struct problem_context *pctx)
227 problem = PR_2_MISSING_DOT_DOT;
228 else if (((dirent->name_len & 0xFF) != 2) ||
229 (dirent->name[0] != '.') ||
230 (dirent->name[1] != '.'))
231 problem = PR_2_2ND_NOT_DOT_DOT;
232 else if (dirent->name[2] != '\0')
233 problem = PR_2_DOT_DOT_NULL_TERM;
236 if (fix_problem(ctx, problem, pctx)) {
237 if (dirent->rec_len < 12)
238 dirent->rec_len = 12;
240 * Note: we don't have the parent inode just
241 * yet, so we will fill it in with the root
242 * inode. This will get fixed in pass 3.
244 dirent->inode = EXT2_ROOT_INO;
245 dirent->name_len = 2;
246 dirent->name[0] = '.';
247 dirent->name[1] = '.';
248 dirent->name[2] = '\0';
253 dir->dotdot = dirent->inode;
258 * Check to make sure a directory entry doesn't contain any illegal
261 static int check_name(e2fsck_t ctx,
262 struct ext2_dir_entry *dirent,
263 ext2_ino_t dir_ino, struct problem_context *pctx)
269 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
270 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
272 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
275 dirent->name[i] = '.';
284 * Check the directory filetype (if present)
286 static _INLINE_ int check_filetype(e2fsck_t ctx,
287 struct ext2_dir_entry *dirent,
288 ext2_ino_t dir_ino, struct problem_context *pctx)
290 int filetype = dirent->name_len >> 8;
291 int should_be = EXT2_FT_UNKNOWN;
292 struct ext2_inode inode;
294 if (!(ctx->fs->super->s_feature_incompat &
295 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
297 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
299 dirent->name_len = dirent->name_len & 0xFF;
303 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dirent->inode)) {
304 should_be = EXT2_FT_DIR;
305 } else if (ext2fs_test_inode_bitmap(ctx->inode_reg_map,
307 should_be = EXT2_FT_REG_FILE;
308 } else if (ctx->inode_bad_map &&
309 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
313 e2fsck_read_inode(ctx, dirent->inode, &inode,
315 should_be = ext2_file_type(inode.i_mode);
317 if (filetype == should_be)
319 pctx->num = should_be;
321 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
325 dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8;
330 static int check_dir_block(ext2_filsys fs,
331 struct ext2_db_entry *db,
334 struct dir_info *subdir, *dir;
335 struct ext2_dir_entry *dirent;
337 int dir_modified = 0;
339 blk_t block_nr = db->blk;
340 ext2_ino_t ino = db->ino;
342 struct check_dir_struct *cd;
347 cd = (struct check_dir_struct *) priv_data;
352 if ((ctx->progress)(ctx, 2, cd->count++, cd->max))
356 * Make sure the inode is still in use (could have been
357 * deleted in the duplicate/bad blocks pass.
359 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino)))
363 cd->pctx.blk = block_nr;
364 cd->pctx.blkcount = db->blockcnt;
370 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
381 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
385 cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
386 if (cd->pctx.errcode) {
387 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
388 ctx->flags |= E2F_FLAG_ABORT;
391 memset(buf, 0, fs->blocksize);
397 dirent = (struct ext2_dir_entry *) (buf + offset);
398 cd->pctx.dirent = dirent;
399 cd->pctx.num = offset;
400 if (((offset + dirent->rec_len) > fs->blocksize) ||
401 (dirent->rec_len < 12) ||
402 ((dirent->rec_len % 4) != 0) ||
403 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
404 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
405 dirent->rec_len = fs->blocksize - offset;
406 dirent->name_len = 0;
412 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
413 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
414 dirent->name_len = EXT2_NAME_LEN;
419 if (dot_state == 1) {
420 if (check_dot(ctx, dirent, ino, &cd->pctx))
422 } else if (dot_state == 2) {
423 dir = e2fsck_get_dir_info(ctx, ino);
425 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
426 ctx->flags |= E2F_FLAG_ABORT;
429 if (check_dotdot(ctx, dirent, dir, &cd->pctx))
431 } else if (dirent->inode == ino) {
432 problem = PR_2_LINK_DOT;
433 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
443 * Make sure the inode listed is a legal one.
445 if (((dirent->inode != EXT2_ROOT_INO) &&
446 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
447 (dirent->inode > fs->super->s_inodes_count)) {
448 problem = PR_2_BAD_INO;
449 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
452 * If the inode is unused, offer to clear it.
454 problem = PR_2_UNUSED_INODE;
455 } else if (ctx->inode_bb_map &&
456 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
459 * If the inode is in a bad block, offer to
462 problem = PR_2_BB_INODE;
463 } else if ((dot_state > 2) &&
464 ((dirent->name_len & 0xFF) == 1) &&
465 (dirent->name[0] == '.')) {
467 * If there's a '.' entry in anything other
468 * than the first directory entry, it's a
469 * duplicate entry that should be removed.
471 problem = PR_2_DUP_DOT;
472 } else if ((dot_state > 2) &&
473 ((dirent->name_len & 0xFF) == 2) &&
474 (dirent->name[0] == '.') &&
475 (dirent->name[1] == '.')) {
477 * If there's a '..' entry in anything other
478 * than the second directory entry, it's a
479 * duplicate entry that should be removed.
481 problem = PR_2_DUP_DOT_DOT;
482 } else if ((dot_state > 2) &&
483 (dirent->inode == EXT2_ROOT_INO)) {
485 * Don't allow links to the root directory.
486 * We check this specially to make sure we
487 * catch this error case even if the root
488 * directory hasn't been created yet.
490 problem = PR_2_LINK_ROOT;
491 } else if ((dot_state > 2) &&
492 (dirent->name_len & 0xFF) == 0) {
494 * Don't allow zero-length directory names.
496 problem = PR_2_NULL_NAME;
500 if (fix_problem(ctx, problem, &cd->pctx)) {
505 ext2fs_unmark_valid(fs);
506 if (problem == PR_2_BAD_INO)
512 * If the inode was marked as having bad fields in
513 * pass1, process it and offer to fix/clear it.
514 * (We wait until now so that we can display the
515 * pathname to the user.)
517 if (ctx->inode_bad_map &&
518 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
520 if (e2fsck_process_bad_inode(ctx, ino,
526 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
530 if (check_name(ctx, dirent, ino, &cd->pctx))
533 if (check_filetype(ctx, dirent, ino, &cd->pctx))
537 * If this is a directory, then mark its parent in its
538 * dir_info structure. If the parent field is already
539 * filled in, then this directory has more than one
540 * hard link. We assume the first link is correct,
541 * and ask the user if he/she wants to clear this one.
543 if ((dot_state > 2) &&
544 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
546 subdir = e2fsck_get_dir_info(ctx, dirent->inode);
548 cd->pctx.ino = dirent->inode;
549 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
550 ctx->flags |= E2F_FLAG_ABORT;
553 if (subdir->parent) {
554 cd->pctx.ino2 = subdir->parent;
555 if (fix_problem(ctx, PR_2_LINK_DIR,
563 subdir->parent = ino;
566 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
569 ctx->fs_links_count++;
570 ctx->fs_total_count++;
572 offset += dirent->rec_len;
573 } while (offset < fs->blocksize);
577 if (offset != fs->blocksize) {
578 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
579 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
580 dirent->rec_len = cd->pctx.num;
585 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
586 if (cd->pctx.errcode) {
587 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
589 ctx->flags |= E2F_FLAG_ABORT;
593 ext2fs_mark_changed(fs);
599 * This function is called to deallocate a block, and is an interator
600 * functioned called by deallocate inode via ext2fs_iterate_block().
602 static int deallocate_inode_block(ext2_filsys fs,
604 e2_blkcnt_t blockcnt,
609 e2fsck_t ctx = (e2fsck_t) priv_data;
611 if (HOLE_BLKADDR(*block_nr))
613 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
614 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
619 * This fuction deallocates an inode
621 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
623 ext2_filsys fs = ctx->fs;
624 struct ext2_inode inode;
625 struct problem_context pctx;
627 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
628 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
629 inode.i_links_count = 0;
630 inode.i_dtime = time(0);
631 e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
632 clear_problem_context(&pctx);
636 * Fix up the bitmaps...
638 e2fsck_read_bitmaps(ctx);
639 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
640 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
641 if (ctx->inode_bad_map)
642 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
643 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
644 ext2fs_mark_ib_dirty(fs);
646 if (!ext2fs_inode_has_valid_blocks(&inode))
649 ext2fs_mark_bb_dirty(fs);
650 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
651 deallocate_inode_block, ctx);
653 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
654 ctx->flags |= E2F_FLAG_ABORT;
659 extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
662 ext2_filsys fs = ctx->fs;
663 struct ext2_inode inode;
664 int inode_modified = 0;
665 unsigned char *frag, *fsize;
666 struct problem_context pctx;
669 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
671 clear_problem_context(&pctx);
676 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
677 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
678 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
679 !(LINUX_S_ISSOCK(inode.i_mode)))
680 problem = PR_2_BAD_MODE;
682 if (LINUX_S_ISCHR(inode.i_mode)
683 && !e2fsck_pass1_check_device_inode(&inode))
684 problem = PR_2_BAD_CHAR_DEV;
686 if (LINUX_S_ISBLK(inode.i_mode)
687 && !e2fsck_pass1_check_device_inode(&inode))
688 problem = PR_2_BAD_BLOCK_DEV;
690 if (LINUX_S_ISFIFO(inode.i_mode)
691 && !e2fsck_pass1_check_device_inode(&inode))
692 problem = PR_2_BAD_FIFO;
694 if (LINUX_S_ISSOCK(inode.i_mode)
695 && !e2fsck_pass1_check_device_inode(&inode))
696 problem = PR_2_BAD_SOCKET;
699 if (fix_problem(ctx, problem, &pctx)) {
700 deallocate_inode(ctx, ino, 0);
701 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
709 fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
714 switch (fs->super->s_creator_os) {
716 frag = &inode.osd2.linux2.l_i_frag;
717 fsize = &inode.osd2.linux2.l_i_fsize;
720 frag = &inode.osd2.hurd2.h_i_frag;
721 fsize = &inode.osd2.hurd2.h_i_fsize;
724 frag = &inode.osd2.masix2.m_i_frag;
725 fsize = &inode.osd2.masix2.m_i_fsize;
732 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
738 if (fsize && *fsize) {
740 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
747 if (inode.i_file_acl &&
748 fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
749 inode.i_file_acl = 0;
752 if (inode.i_dir_acl &&
753 LINUX_S_ISDIR(inode.i_mode) &&
754 fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
759 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
765 * allocate_dir_block --- this function allocates a new directory
766 * block for a particular inode; this is done if a directory has
767 * a "hole" in it, or if a directory has a illegal block number
768 * that was zeroed out and now needs to be replaced.
770 static int allocate_dir_block(e2fsck_t ctx,
771 struct ext2_db_entry *db,
772 char *buf, struct problem_context *pctx)
774 ext2_filsys fs = ctx->fs;
777 struct ext2_inode inode;
779 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
783 * Read the inode and block bitmaps in; we'll be messing with
786 e2fsck_read_bitmaps(ctx);
789 * First, find a free block
791 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
793 pctx->str = "ext2fs_new_block";
794 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
797 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
798 ext2fs_mark_block_bitmap(fs->block_map, blk);
799 ext2fs_mark_bb_dirty(fs);
802 * Now let's create the actual data block for the inode
805 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
807 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
808 EXT2_ROOT_INO, &block);
811 pctx->str = "ext2fs_new_dir_block";
812 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
816 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
817 ext2fs_free_mem((void **) &block);
819 pctx->str = "ext2fs_write_dir_block";
820 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
825 * Update the inode block count
827 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
828 inode.i_blocks += fs->blocksize / 512;
829 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
830 inode.i_size = (db->blockcnt+1) * fs->blocksize;
831 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
834 * Finally, update the block pointers for the inode
837 pctx->errcode = ext2fs_block_iterate2(fs, db->ino, BLOCK_FLAG_HOLE,
838 0, update_dir_block, db);
840 pctx->str = "ext2fs_block_iterate";
841 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
849 * This is a helper function for allocate_dir_block().
851 static int update_dir_block(ext2_filsys fs,
853 e2_blkcnt_t blockcnt,
858 struct ext2_db_entry *db;
860 db = (struct ext2_db_entry *) priv_data;
861 if (db->blockcnt == (int) blockcnt) {
863 return BLOCK_CHANGED;