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
47 * Keeps track of how many times an inode is referenced.
49 static void deallocate_inode(e2fsck_t ctx, ino_t ino,
51 static int process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino);
52 static int check_dir_block(ext2_filsys fs,
53 struct ext2_db_entry *dir_blocks_info,
55 static int allocate_dir_block(e2fsck_t ctx,
56 struct ext2_db_entry *dir_blocks_info,
57 char *buf, struct problem_context *pctx);
58 static int update_dir_block(ext2_filsys fs,
63 struct check_dir_struct {
65 struct problem_context pctx;
70 void e2fsck_pass2(e2fsck_t ctx)
72 ext2_filsys fs = ctx->fs;
75 struct resource_track rtrack;
78 struct check_dir_struct cd;
81 init_resource_track(&rtrack);
84 clear_problem_context(&cd.pctx);
87 mtrace_print("Pass 2");
90 if (!(ctx->options & E2F_OPT_PREEN))
91 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
93 cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
94 0, ctx->inode_link_info,
96 if (cd.pctx.errcode) {
97 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
98 ctx->flags |= E2F_FLAG_ABORT;
101 buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
102 "directory scan buffer");
105 * Set up the parent pointer for the root directory, if
106 * present. (If the root directory is not present, we will
107 * create it in pass 3.)
109 dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO);
111 dir->parent = EXT2_ROOT_INO;
116 cd.max = ext2fs_dblist_count(fs->dblist);
119 (void) (ctx->progress)(ctx, 2, 0, cd.max);
121 cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
123 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
125 if (cd.pctx.errcode) {
126 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
127 ctx->flags |= E2F_FLAG_ABORT;
131 ext2fs_free_mem((void **) &buf);
132 ext2fs_free_dblist(fs->dblist);
134 if (ctx->inode_bad_map) {
135 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
136 ctx->inode_bad_map = 0;
138 #ifdef RESOURCE_TRACK
139 if (ctx->options & E2F_OPT_TIME2)
140 print_resource_track("Pass 2", &rtrack);
145 * Make sure the first entry in the directory is '.', and that the
146 * directory entry is sane.
148 static int check_dot(e2fsck_t ctx,
149 struct ext2_dir_entry *dirent,
150 ino_t ino, struct problem_context *pctx)
152 struct ext2_dir_entry *nextdir;
159 problem = PR_2_MISSING_DOT;
160 else if (((dirent->name_len & 0xFF) != 1) ||
161 (dirent->name[0] != '.'))
162 problem = PR_2_1ST_NOT_DOT;
163 else if (dirent->name[1] != '\0')
164 problem = PR_2_DOT_NULL_TERM;
167 if (fix_problem(ctx, problem, pctx)) {
168 if (dirent->rec_len < 12)
169 dirent->rec_len = 12;
171 dirent->name_len = 1;
172 dirent->name[0] = '.';
173 dirent->name[1] = '\0';
178 if (dirent->inode != ino) {
179 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
184 if (dirent->rec_len > 12) {
185 new_len = dirent->rec_len - 12;
188 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
189 nextdir = (struct ext2_dir_entry *)
190 ((char *) dirent + 12);
191 dirent->rec_len = 12;
192 nextdir->rec_len = new_len;
194 nextdir->name_len = 0;
203 * Make sure the second entry in the directory is '..', and that the
204 * directory entry is sane. We do not check the inode number of '..'
205 * here; this gets done in pass 3.
207 static int check_dotdot(e2fsck_t ctx,
208 struct ext2_dir_entry *dirent,
209 struct dir_info *dir, struct problem_context *pctx)
214 problem = PR_2_MISSING_DOT_DOT;
215 else if (((dirent->name_len & 0xFF) != 2) ||
216 (dirent->name[0] != '.') ||
217 (dirent->name[1] != '.'))
218 problem = PR_2_2ND_NOT_DOT_DOT;
219 else if (dirent->name[2] != '\0')
220 problem = PR_2_DOT_DOT_NULL_TERM;
223 if (fix_problem(ctx, problem, pctx)) {
224 if (dirent->rec_len < 12)
225 dirent->rec_len = 12;
227 * Note: we don't have the parent inode just
228 * yet, so we will fill it in with the root
229 * inode. This will get fixed in pass 3.
231 dirent->inode = EXT2_ROOT_INO;
232 dirent->name_len = 2;
233 dirent->name[0] = '.';
234 dirent->name[1] = '.';
235 dirent->name[2] = '\0';
240 dir->dotdot = dirent->inode;
245 * Check to make sure a directory entry doesn't contain any illegal
248 static int check_name(e2fsck_t ctx,
249 struct ext2_dir_entry *dirent,
250 ino_t dir_ino, struct problem_context *pctx)
256 for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
257 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
259 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
262 dirent->name[i] = '.';
270 static int check_dir_block(ext2_filsys fs,
271 struct ext2_db_entry *db,
274 struct dir_info *subdir, *dir;
275 struct ext2_dir_entry *dirent;
277 int dir_modified = 0;
279 blk_t block_nr = db->blk;
282 struct check_dir_struct *cd;
287 cd = (struct check_dir_struct *) priv_data;
292 if ((ctx->progress)(ctx, 2, cd->count++, cd->max))
296 * Make sure the inode is still in use (could have been
297 * deleted in the duplicate/bad blocks pass.
299 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino)))
303 cd->pctx.blk = block_nr;
304 cd->pctx.blkcount = db->blockcnt;
310 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
321 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
325 cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
326 if (cd->pctx.errcode) {
327 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
328 ctx->flags |= E2F_FLAG_ABORT;
331 memset(buf, 0, fs->blocksize);
337 dirent = (struct ext2_dir_entry *) (buf + offset);
338 cd->pctx.dirent = dirent;
339 cd->pctx.num = offset;
340 if (((offset + dirent->rec_len) > fs->blocksize) ||
341 (dirent->rec_len < 8) ||
342 ((dirent->rec_len % 4) != 0) ||
343 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
344 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
345 dirent->rec_len = fs->blocksize - offset;
346 dirent->name_len = 0;
352 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
353 if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
354 dirent->name_len = EXT2_NAME_LEN;
359 if (dot_state == 1) {
360 if (check_dot(ctx, dirent, ino, &cd->pctx))
362 } else if (dot_state == 2) {
363 dir = e2fsck_get_dir_info(ctx, ino);
365 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
366 ctx->flags |= E2F_FLAG_ABORT;
369 if (check_dotdot(ctx, dirent, dir, &cd->pctx))
371 } else if (dirent->inode == ino) {
372 problem = PR_2_LINK_DOT;
373 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
383 * Make sure the inode listed is a legal one.
385 if (((dirent->inode != EXT2_ROOT_INO) &&
386 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
387 (dirent->inode > fs->super->s_inodes_count)) {
388 problem = PR_2_BAD_INO;
389 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
392 * If the inode is unused, offer to clear it.
394 problem = PR_2_UNUSED_INODE;
395 } else if (ctx->inode_bb_map &&
396 (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
399 * If the inode is in a bad block, offer to
402 problem = PR_2_BB_INODE;
403 } else if ((dot_state > 2) &&
404 ((dirent->name_len & 0xFF) == 1) &&
405 (dirent->name[0] == '.')) {
407 * If there's a '.' entry in anything other
408 * than the first directory entry, it's a
409 * duplicate entry that should be removed.
411 problem = PR_2_DUP_DOT;
412 } else if ((dot_state > 2) &&
413 ((dirent->name_len & 0xFF) == 2) &&
414 (dirent->name[0] == '.') &&
415 (dirent->name[1] == '.')) {
417 * If there's a '..' entry in anything other
418 * than the second directory entry, it's a
419 * duplicate entry that should be removed.
421 problem = PR_2_DUP_DOT_DOT;
422 } else if ((dot_state > 2) &&
423 (dirent->inode == EXT2_ROOT_INO)) {
425 * Don't allow links to the root directory.
426 * We check this specially to make sure we
427 * catch this error case even if the root
428 * directory hasn't been created yet.
430 problem = PR_2_LINK_ROOT;
434 if (fix_problem(ctx, problem, &cd->pctx)) {
439 ext2fs_unmark_valid(fs);
440 if (problem == PR_2_BAD_INO)
446 * If the inode was marked as having bad fields in
447 * pass1, process it and offer to fix/clear it.
448 * (We wait until now so that we can display the
449 * pathname to the user.)
451 if (ctx->inode_bad_map &&
452 ext2fs_test_inode_bitmap(ctx->inode_bad_map,
454 if (process_bad_inode(ctx, ino, dirent->inode)) {
459 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
463 if (check_name(ctx, dirent, ino, &cd->pctx))
467 * If this is a directory, then mark its parent in its
468 * dir_info structure. If the parent field is already
469 * filled in, then this directory has more than one
470 * hard link. We assume the first link is correct,
471 * and ask the user if he/she wants to clear this one.
473 if ((dot_state > 2) &&
474 (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
476 subdir = e2fsck_get_dir_info(ctx, dirent->inode);
478 cd->pctx.ino = dirent->inode;
479 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
480 ctx->flags |= E2F_FLAG_ABORT;
483 if (subdir->parent) {
484 cd->pctx.ino2 = subdir->parent;
485 if (fix_problem(ctx, PR_2_LINK_DIR,
493 subdir->parent = ino;
496 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
499 ctx->fs_links_count++;
500 ctx->fs_total_count++;
502 offset += dirent->rec_len;
503 } while (offset < fs->blocksize);
507 if (offset != fs->blocksize) {
508 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
509 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
510 dirent->rec_len = cd->pctx.num;
515 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
516 if (cd->pctx.errcode) {
517 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
519 ctx->flags |= E2F_FLAG_ABORT;
523 ext2fs_mark_changed(fs);
529 * This function is called to deallocate a block, and is an interator
530 * functioned called by deallocate inode via ext2fs_iterate_block().
532 static int deallocate_inode_block(ext2_filsys fs,
537 e2fsck_t ctx = (e2fsck_t) priv_data;
541 ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
542 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
547 * This fuction deallocates an inode
549 static void deallocate_inode(e2fsck_t ctx, ino_t ino,
552 ext2_filsys fs = ctx->fs;
553 struct ext2_inode inode;
554 struct problem_context pctx;
556 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
557 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
558 inode.i_links_count = 0;
559 inode.i_dtime = time(0);
560 e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
561 clear_problem_context(&pctx);
565 * Fix up the bitmaps...
567 e2fsck_read_bitmaps(ctx);
568 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
569 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
570 if (ctx->inode_bad_map)
571 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
572 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
573 ext2fs_mark_ib_dirty(fs);
575 if (!ext2fs_inode_has_valid_blocks(&inode))
578 ext2fs_mark_bb_dirty(fs);
579 pctx.errcode = ext2fs_block_iterate(fs, ino, 0, block_buf,
580 deallocate_inode_block, ctx);
582 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
583 ctx->flags |= E2F_FLAG_ABORT;
588 static int process_bad_inode(e2fsck_t ctx, ino_t dir, ino_t ino)
590 ext2_filsys fs = ctx->fs;
591 struct ext2_inode inode;
592 int inode_modified = 0;
593 unsigned char *frag, *fsize;
594 struct problem_context pctx;
597 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
599 clear_problem_context(&pctx);
604 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
605 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
606 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
607 !(LINUX_S_ISSOCK(inode.i_mode)))
608 problem = PR_2_BAD_MODE;
610 if (LINUX_S_ISCHR(inode.i_mode)
611 && !e2fsck_pass1_check_device_inode(&inode))
612 problem = PR_2_BAD_CHAR_DEV;
614 if (LINUX_S_ISBLK(inode.i_mode)
615 && !e2fsck_pass1_check_device_inode(&inode))
616 problem = PR_2_BAD_BLOCK_DEV;
618 if (LINUX_S_ISFIFO(inode.i_mode)
619 && !e2fsck_pass1_check_device_inode(&inode))
620 problem = PR_2_BAD_FIFO;
622 if (LINUX_S_ISSOCK(inode.i_mode)
623 && !e2fsck_pass1_check_device_inode(&inode))
624 problem = PR_2_BAD_SOCKET;
627 if (fix_problem(ctx, problem, &pctx)) {
628 deallocate_inode(ctx, ino, 0);
629 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
637 fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
642 switch (fs->super->s_creator_os) {
644 frag = &inode.osd2.linux2.l_i_frag;
645 fsize = &inode.osd2.linux2.l_i_fsize;
648 frag = &inode.osd2.hurd2.h_i_frag;
649 fsize = &inode.osd2.hurd2.h_i_fsize;
652 frag = &inode.osd2.masix2.m_i_frag;
653 fsize = &inode.osd2.masix2.m_i_fsize;
660 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
666 if (fsize && *fsize) {
668 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
675 if (inode.i_file_acl &&
676 fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
677 inode.i_file_acl = 0;
680 if (inode.i_dir_acl &&
681 LINUX_S_ISDIR(inode.i_mode) &&
682 fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
687 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
693 * allocate_dir_block --- this function allocates a new directory
694 * block for a particular inode; this is done if a directory has
695 * a "hole" in it, or if a directory has a illegal block number
696 * that was zeroed out and now needs to be replaced.
698 static int allocate_dir_block(e2fsck_t ctx,
699 struct ext2_db_entry *db,
700 char *buf, struct problem_context *pctx)
702 ext2_filsys fs = ctx->fs;
705 struct ext2_inode inode;
707 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
711 * Read the inode and block bitmaps in; we'll be messing with
714 e2fsck_read_bitmaps(ctx);
717 * First, find a free block
719 pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
721 pctx->str = "ext2fs_new_block";
722 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
725 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
726 ext2fs_mark_block_bitmap(fs->block_map, blk);
727 ext2fs_mark_bb_dirty(fs);
730 * Now let's create the actual data block for the inode
733 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
735 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
736 EXT2_ROOT_INO, &block);
739 pctx->str = "ext2fs_new_dir_block";
740 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
744 pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
745 ext2fs_free_mem((void **) &block);
747 pctx->str = "ext2fs_write_dir_block";
748 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
753 * Update the inode block count
755 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
756 inode.i_blocks += fs->blocksize / 512;
757 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
758 inode.i_size = (db->blockcnt+1) * fs->blocksize;
759 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
762 * Finally, update the block pointers for the inode
765 pctx->errcode = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
766 0, update_dir_block, db);
768 pctx->str = "ext2fs_block_iterate";
769 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
777 * This is a helper function for allocate_dir_block().
779 static int update_dir_block(ext2_filsys fs,
784 struct ext2_db_entry *db;
786 db = (struct ext2_db_entry *) priv_data;
787 if (db->blockcnt == blockcnt) {
789 return BLOCK_CHANGED;