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
43 #include "et/com_err.h"
49 * Keeps track of how many times an inode is referenced.
51 ext2_icount_t inode_count = 0;
53 static void deallocate_inode(ext2_filsys fs, ino_t ino,
55 static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino);
56 static int check_dir_block(ext2_filsys fs,
57 struct ext2_db_entry *dir_blocks_info,
59 static int allocate_dir_block(ext2_filsys fs,
60 struct ext2_db_entry *dir_blocks_info,
61 char *buf, struct problem_context *pctx);
62 static int update_dir_block(ext2_filsys fs,
67 struct check_dir_struct {
69 struct problem_context pctx;
72 void pass2(ext2_filsys fs)
75 struct resource_track rtrack;
79 struct check_dir_struct cd;
81 init_resource_track(&rtrack);
84 mtrace_print("Pass 2");
88 printf("Pass 2: Checking directory structure\n");
89 retval = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
90 0, inode_link_info, &inode_count);
92 com_err("ext2fs_create_icount", retval,
93 "while creating inode_count");
96 buf = allocate_memory(fs->blocksize, "directory scan buffer");
99 * Set up the parent pointer for the root directory, if
100 * present. (If the root directory is not present, we will
101 * create it in pass 3.)
103 dir = get_dir_info(EXT2_ROOT_INO);
105 dir->parent = EXT2_ROOT_INO;
108 clear_problem_context(&cd.pctx);
110 retval = ext2fs_dblist_iterate(fs->dblist, check_dir_block, &cd);
112 com_err("ext2fs_dblist_iterate", retval,
113 "while iterating through dblist");
118 ext2fs_free_dblist(fs->dblist);
121 ext2fs_free_inode_bitmap(inode_bad_map);
126 print_resource_track(&rtrack);
131 * Make sure the first entry in the directory is '.', and that the
132 * directory entry is sane.
134 static int check_dot(ext2_filsys fs,
135 struct ext2_dir_entry *dirent,
136 ino_t ino, struct problem_context *pctx)
138 struct ext2_dir_entry *nextdir;
145 problem = PR_2_MISSING_DOT;
146 else if ((dirent->name_len != 1) ||
147 (dirent->name[0] != '.'))
148 problem = PR_2_1ST_NOT_DOT;
149 else if (dirent->name[1] != '\0')
150 problem = PR_2_DOT_NULL_TERM;
153 if (fix_problem(fs, problem, pctx)) {
154 if (dirent->rec_len < 12)
155 dirent->rec_len = 12;
157 dirent->name_len = 1;
158 dirent->name[0] = '.';
159 dirent->name[1] = '\0';
164 if (dirent->inode != ino) {
165 if (fix_problem(fs, PR_2_BAD_INODE_DOT, pctx)) {
170 if (dirent->rec_len > 12) {
171 new_len = dirent->rec_len - 12;
175 ask("Directory entry for '.' is big. Split", 1)) {
176 nextdir = (struct ext2_dir_entry *)
177 ((char *) dirent + 12);
178 dirent->rec_len = 12;
179 nextdir->rec_len = new_len;
181 nextdir->name_len = 0;
190 * Make sure the second entry in the directory is '..', and that the
191 * directory entry is sane. We do not check the inode number of '..'
192 * here; this gets done in pass 3.
194 static int check_dotdot(ext2_filsys fs,
195 struct ext2_dir_entry *dirent,
196 struct dir_info *dir, struct problem_context *pctx)
201 problem = PR_2_MISSING_DOT_DOT;
202 else if ((dirent->name_len != 2) ||
203 (dirent->name[0] != '.') ||
204 (dirent->name[1] != '.'))
205 problem = PR_2_2ND_NOT_DOT_DOT;
206 else if (dirent->name[2] != '\0')
207 problem = PR_2_DOT_DOT_NULL_TERM;
210 if (fix_problem(fs, problem, pctx)) {
211 if (dirent->rec_len < 12)
212 dirent->rec_len = 12;
214 * Note: we don't have the parent inode just
215 * yet, so we will fill it in with the root
216 * inode. This will get fixed in pass 3.
218 dirent->inode = EXT2_ROOT_INO;
219 dirent->name_len = 2;
220 dirent->name[0] = '.';
221 dirent->name[1] = '.';
222 dirent->name[2] = '\0';
227 dir->dotdot = dirent->inode;
232 * Check to make sure a directory entry doesn't contain any illegal
235 static int check_name(ext2_filsys fs,
236 struct ext2_dir_entry *dirent,
237 ino_t dir_ino, struct problem_context *pctx)
243 for ( i = 0; i < dirent->name_len; i++) {
244 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
246 fixup = fix_problem(fs, PR_2_BAD_NAME, pctx);
249 dirent->name[i] = '.';
257 static int check_dir_block(ext2_filsys fs,
258 struct ext2_db_entry *db,
261 struct dir_info *subdir, *dir;
262 struct ext2_dir_entry *dirent;
264 int dir_modified = 0;
267 blk_t block_nr = db->blk;
270 struct check_dir_struct *cd = private;
274 * Make sure the inode is still in use (could have been
275 * deleted in the duplicate/bad blocks pass.
277 if (!(ext2fs_test_inode_bitmap(inode_used_map, ino)))
281 cd->pctx.blk = block_nr;
282 cd->pctx.blkcount = db->blockcnt;
288 if (allocate_dir_block(fs, db, buf, &cd->pctx))
299 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
303 retval = ext2fs_read_dir_block(fs, block_nr, buf);
305 com_err(program_name, retval,
306 "while reading directory block %d", block_nr);
311 dirent = (struct ext2_dir_entry *) (buf + offset);
312 cd->pctx.dirent = dirent;
313 cd->pctx.num = offset;
314 if (((offset + dirent->rec_len) > fs->blocksize) ||
315 (dirent->rec_len < 8) ||
316 ((dirent->rec_len % 4) != 0) ||
317 ((dirent->name_len+8) > dirent->rec_len)) {
318 if (fix_problem(fs, PR_2_DIR_CORRUPTED, &cd->pctx)) {
319 dirent->rec_len = fs->blocksize - offset;
320 dirent->name_len = 0;
327 if (dirent->name_len > EXT2_NAME_LEN) {
328 if (fix_problem(fs, PR_2_FILENAME_LONG, &cd->pctx)) {
329 dirent->name_len = EXT2_NAME_LEN;
334 if (dot_state == 1) {
335 if (check_dot(fs, dirent, ino, &cd->pctx))
337 } else if (dot_state == 2) {
338 dir = get_dir_info(ino);
340 printf("Internal error: couldn't find dir_info for %lu\n",
344 if (check_dotdot(fs, dirent, dir, &cd->pctx))
346 } else if (dirent->inode == ino) {
347 if (fix_problem(fs, PR_2_LINK_DOT, &cd->pctx)) {
356 if (check_name(fs, dirent, ino, &cd->pctx))
360 * Make sure the inode listed is a legal one.
362 if (((dirent->inode != EXT2_ROOT_INO) &&
363 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
364 (dirent->inode > fs->super->s_inodes_count)) {
365 if (fix_problem(fs, PR_2_BAD_INO, &cd->pctx)) {
373 * If the inode is unused, offer to clear it.
375 if (!(ext2fs_test_inode_bitmap(inode_used_map,
377 if (fix_problem(fs, PR_2_UNUSED_INODE, &cd->pctx)) {
385 * If the inode is in a bad block, offer to clear it.
388 (ext2fs_test_inode_bitmap(inode_bb_map,
390 if (fix_problem(fs, PR_2_BB_INODE, &cd->pctx)) {
398 * If the inode was marked as having bad fields in
399 * pass1, process it and offer to fix/clear it.
400 * (We wait until now so that we can display the
401 * pathname to the user.)
404 ext2fs_test_inode_bitmap(inode_bad_map,
406 if (process_bad_inode(fs, ino, dirent->inode)) {
414 * Don't allow links to the root directory. We check
415 * this specially to make sure we catch this error
416 * case even if the root directory hasn't been created
419 if ((dot_state > 2) && (dirent->inode == EXT2_ROOT_INO)) {
420 if (fix_problem(fs, PR_2_LINK_ROOT, &cd->pctx)) {
428 * If this is a directory, then mark its parent in its
429 * dir_info structure. If the parent field is already
430 * filled in, then this directory has more than one
431 * hard link. We assume the first link is correct,
432 * and ask the user if he/she wants to clear this one.
434 if ((dot_state > 2) &&
435 (ext2fs_test_inode_bitmap(inode_dir_map,
437 subdir = get_dir_info(dirent->inode);
439 printf("INTERNAL ERROR: missing dir %u\n",
443 if (subdir->parent) {
444 cd->pctx.ino2 = subdir->parent;
445 if (fix_problem(fs, PR_2_LINK_DIR,
453 subdir->parent = ino;
456 ext2fs_icount_increment(inode_count, dirent->inode, &links);
461 offset += dirent->rec_len;
462 } while (offset < fs->blocksize);
466 if (offset != fs->blocksize) {
467 printf("Final rec_len is %d, should be %d\n",
469 dirent->rec_len - fs->blocksize + offset);
472 retval = ext2fs_write_dir_block(fs, block_nr, buf);
474 com_err(program_name, retval,
475 "while writing directory block %d", block_nr);
477 ext2fs_mark_changed(fs);
483 * This function is called to deallocate a block, and is an interator
484 * functioned called by deallocate inode via ext2fs_iterate_block().
486 static int deallocate_inode_block(ext2_filsys fs,
493 ext2fs_unmark_block_bitmap(block_found_map, *block_nr);
494 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
499 * This fuction deallocates an inode
501 static void deallocate_inode(ext2_filsys fs, ino_t ino,
505 struct ext2_inode inode;
507 ext2fs_icount_store(inode_link_info, ino, 0);
508 e2fsck_read_inode(fs, ino, &inode, "deallocate_inode");
509 inode.i_links_count = 0;
510 inode.i_dtime = time(0);
511 e2fsck_write_inode(fs, ino, &inode, "deallocate_inode");
514 * Fix up the bitmaps...
517 ext2fs_unmark_inode_bitmap(inode_used_map, ino);
518 ext2fs_unmark_inode_bitmap(inode_dir_map, ino);
520 ext2fs_unmark_inode_bitmap(inode_bad_map, ino);
521 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
522 ext2fs_mark_ib_dirty(fs);
524 if (!ext2fs_inode_has_valid_blocks(&inode))
527 ext2fs_mark_bb_dirty(fs);
528 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
529 deallocate_inode_block, 0);
531 com_err("deallocate_inode", retval,
532 "while calling ext2fs_block_iterate for inode %d",
536 static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino)
538 struct ext2_inode inode;
539 int inode_modified = 0;
540 unsigned char *frag, *fsize;
541 struct problem_context pctx;
543 e2fsck_read_inode(fs, ino, &inode, "process_bad_inode");
545 clear_problem_context(&pctx);
550 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
551 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
552 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
553 !(LINUX_S_ISSOCK(inode.i_mode))) {
554 if (fix_problem(fs, PR_2_BAD_MODE, &pctx)) {
555 deallocate_inode(fs, ino, 0);
560 if (LINUX_S_ISCHR(inode.i_mode)
561 && !e2fsck_pass1_check_device_inode(&inode)) {
562 if (fix_problem(fs, PR_2_BAD_CHAR_DEV, &pctx)) {
563 deallocate_inode(fs, ino, 0);
568 if (LINUX_S_ISBLK(inode.i_mode)
569 && !e2fsck_pass1_check_device_inode(&inode)) {
570 if (fix_problem(fs, PR_2_BAD_BLOCK_DEV, &pctx)) {
571 deallocate_inode(fs, ino, 0);
578 fix_problem(fs, PR_2_FADDR_ZERO, &pctx)) {
583 switch (fs->super->s_creator_os) {
585 frag = &inode.osd2.linux2.l_i_frag;
586 fsize = &inode.osd2.linux2.l_i_fsize;
589 frag = &inode.osd2.hurd2.h_i_frag;
590 fsize = &inode.osd2.hurd2.h_i_fsize;
593 frag = &inode.osd2.masix2.m_i_frag;
594 fsize = &inode.osd2.masix2.m_i_fsize;
601 if (fix_problem(fs, PR_2_FRAG_ZERO, &pctx)) {
607 if (fsize && *fsize) {
609 if (fix_problem(fs, PR_2_FSIZE_ZERO, &pctx)) {
616 if (inode.i_file_acl &&
617 fix_problem(fs, PR_2_FILE_ACL_ZERO, &pctx)) {
618 inode.i_file_acl = 0;
621 if (inode.i_dir_acl &&
622 fix_problem(fs, PR_2_DIR_ACL_ZERO, &pctx)) {
627 e2fsck_write_inode(fs, ino, &inode, "process_bad_inode");
633 * allocate_dir_block --- this function allocates a new directory
634 * block for a particular inode; this is done if a directory has
635 * a "hole" in it, or if a directory has a illegal block number
636 * that was zeroed out and now needs to be replaced.
638 static int allocate_dir_block(ext2_filsys fs,
639 struct ext2_db_entry *db,
640 char *buf, struct problem_context *pctx)
644 struct ext2_inode inode;
647 if (fix_problem(fs, PR_2_DIRECTORY_HOLE, pctx) == 0)
651 * Read the inode and block bitmaps in; we'll be messing with
657 * First, find a free block
659 retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
661 com_err("allocate_dir_block", retval,
662 "while trying to fill a hole in a directory inode");
665 ext2fs_mark_block_bitmap(block_found_map, blk);
666 ext2fs_mark_block_bitmap(fs->block_map, blk);
667 ext2fs_mark_bb_dirty(fs);
670 * Now let's create the actual data block for the inode
673 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
675 retval = ext2fs_new_dir_block(fs, db->ino, EXT2_ROOT_INO,
679 com_err("allocate_dir_block", retval,
680 "while creating new directory block");
684 retval = ext2fs_write_dir_block(fs, blk, block);
687 com_err("allocate_dir_block", retval,
688 "while writing an empty directory block");
693 * Update the inode block count
695 e2fsck_read_inode(fs, db->ino, &inode, "allocate_dir_block");
696 inode.i_blocks += fs->blocksize / 512;
697 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
698 inode.i_size = (db->blockcnt+1) * fs->blocksize;
699 e2fsck_write_inode(fs, db->ino, &inode, "allocate_dir_block");
702 * Finally, update the block pointers for the inode
705 retval = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
706 0, update_dir_block, db);
708 com_err("allocate_dir_block", retval,
709 "while calling ext2fs_block_iterate");
717 * This is a helper function for allocate_dir_block().
719 static int update_dir_block(ext2_filsys fs,
724 struct ext2_db_entry *db = private;
726 if (db->blockcnt == blockcnt) {
728 return BLOCK_CHANGED;