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, ino_t ino,
58 static int check_dir_block(ext2_filsys fs,
59 struct ext2_db_entry *dir_blocks_info,
61 static int allocate_dir_block(e2fsck_t ctx,
62 struct ext2_db_entry *dir_blocks_info,
63 char *buf, struct problem_context *pctx);
64 static int update_dir_block(ext2_filsys fs,
69 struct check_dir_struct {
71 struct problem_context pctx;
76 void e2fsck_pass2(e2fsck_t ctx)
78 ext2_filsys fs = ctx->fs;
81 struct resource_track rtrack;
84 struct check_dir_struct cd;
87 init_resource_track(&rtrack);
90 clear_problem_context(&cd.pctx);
93 mtrace_print("Pass 2");
96 if (!(ctx->options & E2F_OPT_PREEN))
97 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
99 cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
100 0, ctx->inode_link_info,
102 if (cd.pctx.errcode) {
103 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
104 ctx->flags |= E2F_FLAG_ABORT;
107 buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
108 "directory scan buffer");
111 * Set up the parent pointer for the root directory, if
112 * present. (If the root directory is not present, we will
113 * create it in pass 3.)
115 dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO);
117 dir->parent = EXT2_ROOT_INO;
122 cd.max = ext2fs_dblist_count(fs->dblist);
125 (void) (ctx->progress)(ctx, 2, 0, cd.max);
127 cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
129 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
131 if (cd.pctx.errcode) {
132 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
133 ctx->flags |= E2F_FLAG_ABORT;
137 ext2fs_free_mem((void **) &buf);
138 ext2fs_free_dblist(fs->dblist);
140 if (ctx->inode_bad_map) {
141 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
142 ctx->inode_bad_map = 0;
144 if (ctx->inode_reg_map) {
145 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
146 ctx->inode_reg_map = 0;
148 #ifdef RESOURCE_TRACK
149 if (ctx->options & E2F_OPT_TIME2) {
150 e2fsck_clear_progbar(ctx);
151 print_resource_track("Pass 2", &rtrack);
157 * Make sure the first entry in the directory is '.', and that the
158 * directory entry is sane.
160 static int check_dot(e2fsck_t ctx,
161 struct ext2_dir_entry *dirent,
162 ino_t ino, struct problem_context *pctx)
164 struct ext2_dir_entry *nextdir;
171 problem = PR_2_MISSING_DOT;
172 else if (((dirent->name_len & 0xFF) != 1) ||
173 (dirent->name[0] != '.'))
174 problem = PR_2_1ST_NOT_DOT;
175 else if (dirent->name[1] != '\0')
176 problem = PR_2_DOT_NULL_TERM;
179 if (fix_problem(ctx, problem, pctx)) {
180 if (dirent->rec_len < 12)
181 dirent->rec_len = 12;
183 dirent->name_len = 1;
184 dirent->name[0] = '.';
185 dirent->name[1] = '\0';
190 if (dirent->inode != ino) {
191 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
196 if (dirent->rec_len > 12) {
197 new_len = dirent->rec_len - 12;
200 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
201 nextdir = (struct ext2_dir_entry *)
202 ((char *) dirent + 12);
203 dirent->rec_len = 12;
204 nextdir->rec_len = new_len;
206 nextdir->name_len = 0;
215 * Make sure the second entry in the directory is '..', and that the
216 * directory entry is sane. We do not check the inode number of '..'
217 * here; this gets done in pass 3.
219 static int check_dotdot(e2fsck_t ctx,
220 struct ext2_dir_entry *dirent,
221 struct dir_info *dir, struct problem_context *pctx)
226 problem = PR_2_MISSING_DOT_DOT;
227 else if (((dirent->name_len & 0xFF) != 2) ||
228 (dirent->name[0] != '.') ||
229 (dirent->name[1] != '.'))
230 problem = PR_2_2ND_NOT_DOT_DOT;
231 else if (dirent->name[2] != '\0')
232 problem = PR_2_DOT_DOT_NULL_TERM;
235 if (fix_problem(ctx, problem, pctx)) {
236 if (dirent->rec_len < 12)
237 dirent->rec_len = 12;
239 * Note: we don't have the parent inode just
240 * yet, so we will fill it in with the root
241 * inode. This will get fixed in pass 3.
243 dirent->inode = EXT2_ROOT_INO;
244 dirent->name_len = 2;
245 dirent->name[0] = '.';
246 dirent->name[1] = '.';
247 dirent->name[2] = '\0';
252 dir->dotdot = dirent->inode;
257 * Check to make sure a directory entry doesn't contain any illegal
260 static int check_name(e2fsck_t ctx,
261 struct ext2_dir_entry *dirent,
262 ino_t dir_ino, struct problem_context *pctx)
268 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
269 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
271 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
274 dirent->name[i] = '.';
283 * Check the directory filetype (if present)
285 static _INLINE_ int check_filetype(e2fsck_t ctx,
286 struct ext2_dir_entry *dirent,
287 ino_t dir_ino, struct problem_context *pctx)
289 int filetype = dirent->name_len >> 8;
290 int should_be = EXT2_FT_UNKNOWN;
291 struct ext2_inode inode;
293 if (!(ctx->fs->super->s_feature_incompat &
294 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
296 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
298 dirent->name_len = dirent->name_len & 0xFF;
302 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dirent->inode)) {
303 should_be = EXT2_FT_DIR;
304 } else if (ext2fs_test_inode_bitmap(ctx->inode_reg_map,
306 should_be = EXT2_FT_REG_FILE;
307 } else if (ctx->inode_bad_map &&
308 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
312 e2fsck_read_inode(ctx, dirent->inode, &inode,
314 should_be = ext2_file_type(inode.i_mode);
316 if (filetype == should_be)
318 pctx->num = should_be;
320 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
324 dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8;
329 static int check_dir_block(ext2_filsys fs,
330 struct ext2_db_entry *db,
333 struct dir_info *subdir, *dir;
334 struct ext2_dir_entry *dirent;
336 int dir_modified = 0;
338 blk_t block_nr = db->blk;
341 struct check_dir_struct *cd;
346 cd = (struct check_dir_struct *) priv_data;
351 if ((ctx->progress)(ctx, 2, cd->count++, cd->max))
355 * Make sure the inode is still in use (could have been
356 * deleted in the duplicate/bad blocks pass.
358 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino)))
362 cd->pctx.blk = block_nr;
363 cd->pctx.blkcount = db->blockcnt;
369 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
380 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
384 cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
385 if (cd->pctx.errcode) {
386 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
387 ctx->flags |= E2F_FLAG_ABORT;
390 memset(buf, 0, fs->blocksize);
396 dirent = (struct ext2_dir_entry *) (buf + offset);
397 cd->pctx.dirent = dirent;
398 cd->pctx.num = offset;
399 if (((offset + dirent->rec_len) > fs->blocksize) ||
400 (dirent->rec_len < 12) ||
401 ((dirent->rec_len % 4) != 0) ||
402 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
403 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
404 dirent->rec_len = fs->blocksize - offset;
405 dirent->name_len = 0;
411 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
412 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
413 dirent->name_len = EXT2_NAME_LEN;
418 if (dot_state == 1) {
419 if (check_dot(ctx, dirent, ino, &cd->pctx))
421 } else if (dot_state == 2) {
422 dir = e2fsck_get_dir_info(ctx, ino);
424 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
425 ctx->flags |= E2F_FLAG_ABORT;
428 if (check_dotdot(ctx, dirent, dir, &cd->pctx))
430 } else if (dirent->inode == ino) {
431 problem = PR_2_LINK_DOT;
432 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
442 * Make sure the inode listed is a legal one.
444 if (((dirent->inode != EXT2_ROOT_INO) &&
445 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
446 (dirent->inode > fs->super->s_inodes_count)) {
447 problem = PR_2_BAD_INO;
448 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
451 * If the inode is unused, offer to clear it.
453 problem = PR_2_UNUSED_INODE;
454 } else if (ctx->inode_bb_map &&
455 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
458 * If the inode is in a bad block, offer to
461 problem = PR_2_BB_INODE;
462 } else if ((dot_state > 2) &&
463 ((dirent->name_len & 0xFF) == 1) &&
464 (dirent->name[0] == '.')) {
466 * If there's a '.' entry in anything other
467 * than the first directory entry, it's a
468 * duplicate entry that should be removed.
470 problem = PR_2_DUP_DOT;
471 } else if ((dot_state > 2) &&
472 ((dirent->name_len & 0xFF) == 2) &&
473 (dirent->name[0] == '.') &&
474 (dirent->name[1] == '.')) {
476 * If there's a '..' entry in anything other
477 * than the second directory entry, it's a
478 * duplicate entry that should be removed.
480 problem = PR_2_DUP_DOT_DOT;
481 } else if ((dot_state > 2) &&
482 (dirent->inode == EXT2_ROOT_INO)) {
484 * Don't allow links to the root directory.
485 * We check this specially to make sure we
486 * catch this error case even if the root
487 * directory hasn't been created yet.
489 problem = PR_2_LINK_ROOT;
490 } else if ((dot_state > 2) &&
491 (dirent->name_len & 0xFF) == 0) {
493 * Don't allow zero-length directory names.
495 problem = PR_2_NULL_NAME;
499 if (fix_problem(ctx, problem, &cd->pctx)) {
504 ext2fs_unmark_valid(fs);
505 if (problem == PR_2_BAD_INO)
511 * If the inode was marked as having bad fields in
512 * pass1, process it and offer to fix/clear it.
513 * (We wait until now so that we can display the
514 * pathname to the user.)
516 if (ctx->inode_bad_map &&
517 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
519 if (e2fsck_process_bad_inode(ctx, ino,
525 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
529 if (check_name(ctx, dirent, ino, &cd->pctx))
532 if (check_filetype(ctx, dirent, ino, &cd->pctx))
536 * If this is a directory, then mark its parent in its
537 * dir_info structure. If the parent field is already
538 * filled in, then this directory has more than one
539 * hard link. We assume the first link is correct,
540 * and ask the user if he/she wants to clear this one.
542 if ((dot_state > 2) &&
543 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
545 subdir = e2fsck_get_dir_info(ctx, dirent->inode);
547 cd->pctx.ino = dirent->inode;
548 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
549 ctx->flags |= E2F_FLAG_ABORT;
552 if (subdir->parent) {
553 cd->pctx.ino2 = subdir->parent;
554 if (fix_problem(ctx, PR_2_LINK_DIR,
562 subdir->parent = ino;
565 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
568 ctx->fs_links_count++;
569 ctx->fs_total_count++;
571 offset += dirent->rec_len;
572 } while (offset < fs->blocksize);
576 if (offset != fs->blocksize) {
577 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
578 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
579 dirent->rec_len = cd->pctx.num;
584 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
585 if (cd->pctx.errcode) {
586 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
588 ctx->flags |= E2F_FLAG_ABORT;
592 ext2fs_mark_changed(fs);
598 * This function is called to deallocate a block, and is an interator
599 * functioned called by deallocate inode via ext2fs_iterate_block().
601 static int deallocate_inode_block(ext2_filsys fs,
606 e2fsck_t ctx = (e2fsck_t) priv_data;
608 if (HOLE_BLKADDR(*block_nr))
610 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
611 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
616 * This fuction deallocates an inode
618 static void deallocate_inode(e2fsck_t ctx, ino_t ino,
621 ext2_filsys fs = ctx->fs;
622 struct ext2_inode inode;
623 struct problem_context pctx;
625 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
626 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
627 inode.i_links_count = 0;
628 inode.i_dtime = time(0);
629 e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
630 clear_problem_context(&pctx);
634 * Fix up the bitmaps...
636 e2fsck_read_bitmaps(ctx);
637 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
638 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
639 if (ctx->inode_bad_map)
640 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
641 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
642 ext2fs_mark_ib_dirty(fs);
644 if (!ext2fs_inode_has_valid_blocks(&inode))
647 ext2fs_mark_bb_dirty(fs);
648 pctx.errcode = ext2fs_block_iterate(fs, ino, 0, block_buf,
649 deallocate_inode_block, ctx);
651 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
652 ctx->flags |= E2F_FLAG_ABORT;
657 extern int e2fsck_process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino)
659 ext2_filsys fs = ctx->fs;
660 struct ext2_inode inode;
661 int inode_modified = 0;
662 unsigned char *frag, *fsize;
663 struct problem_context pctx;
666 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
668 clear_problem_context(&pctx);
673 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
674 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
675 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
676 !(LINUX_S_ISSOCK(inode.i_mode)))
677 problem = PR_2_BAD_MODE;
679 if (LINUX_S_ISCHR(inode.i_mode)
680 && !e2fsck_pass1_check_device_inode(&inode))
681 problem = PR_2_BAD_CHAR_DEV;
683 if (LINUX_S_ISBLK(inode.i_mode)
684 && !e2fsck_pass1_check_device_inode(&inode))
685 problem = PR_2_BAD_BLOCK_DEV;
687 if (LINUX_S_ISFIFO(inode.i_mode)
688 && !e2fsck_pass1_check_device_inode(&inode))
689 problem = PR_2_BAD_FIFO;
691 if (LINUX_S_ISSOCK(inode.i_mode)
692 && !e2fsck_pass1_check_device_inode(&inode))
693 problem = PR_2_BAD_SOCKET;
696 if (fix_problem(ctx, problem, &pctx)) {
697 deallocate_inode(ctx, ino, 0);
698 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
706 fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
711 switch (fs->super->s_creator_os) {
713 frag = &inode.osd2.linux2.l_i_frag;
714 fsize = &inode.osd2.linux2.l_i_fsize;
717 frag = &inode.osd2.hurd2.h_i_frag;
718 fsize = &inode.osd2.hurd2.h_i_fsize;
721 frag = &inode.osd2.masix2.m_i_frag;
722 fsize = &inode.osd2.masix2.m_i_fsize;
729 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
735 if (fsize && *fsize) {
737 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
744 if (inode.i_file_acl &&
745 fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
746 inode.i_file_acl = 0;
749 if (inode.i_dir_acl &&
750 LINUX_S_ISDIR(inode.i_mode) &&
751 fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
756 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
762 * allocate_dir_block --- this function allocates a new directory
763 * block for a particular inode; this is done if a directory has
764 * a "hole" in it, or if a directory has a illegal block number
765 * that was zeroed out and now needs to be replaced.
767 static int allocate_dir_block(e2fsck_t ctx,
768 struct ext2_db_entry *db,
769 char *buf, struct problem_context *pctx)
771 ext2_filsys fs = ctx->fs;
774 struct ext2_inode inode;
776 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
780 * Read the inode and block bitmaps in; we'll be messing with
783 e2fsck_read_bitmaps(ctx);
786 * First, find a free block
788 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
790 pctx->str = "ext2fs_new_block";
791 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
794 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
795 ext2fs_mark_block_bitmap(fs->block_map, blk);
796 ext2fs_mark_bb_dirty(fs);
799 * Now let's create the actual data block for the inode
802 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
804 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
805 EXT2_ROOT_INO, &block);
808 pctx->str = "ext2fs_new_dir_block";
809 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
813 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
814 ext2fs_free_mem((void **) &block);
816 pctx->str = "ext2fs_write_dir_block";
817 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
822 * Update the inode block count
824 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
825 inode.i_blocks += fs->blocksize / 512;
826 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
827 inode.i_size = (db->blockcnt+1) * fs->blocksize;
828 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
831 * Finally, update the block pointers for the inode
834 pctx->errcode = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
835 0, update_dir_block, db);
837 pctx->str = "ext2fs_block_iterate";
838 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
846 * This is a helper function for allocate_dir_block().
848 static int update_dir_block(ext2_filsys fs,
853 struct ext2_db_entry *db;
855 db = (struct ext2_db_entry *) priv_data;
856 if (db->blockcnt == blockcnt) {
858 return BLOCK_CHANGED;