2 * pass2.c --- check directory structure
4 * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
7 * Pass 2 of e2fsck iterates through all active directory inodes, and
8 * applies to following tests to each directory entry in the directory
9 * blocks in the inodes:
11 * - The length of the directory entry (rec_len) should be at
12 * least 8 bytes, and no more than the remaining space
13 * left in the directory block.
14 * - The length of the name in the directory entry (name_len)
15 * should be less than (rec_len - 8).
16 * - The inode number in the directory entry should be within
18 * - The inode number should refer to a in-use inode.
19 * - The first entry should be '.', and its inode should be
20 * the inode of the directory.
21 * - The second entry should be '..'.
23 * To minimize disk seek time, the directory blocks are processed in
24 * sorted order of block numbers.
26 * Pass 2 also collects the following information:
27 * - The inode numbers of the subdirectories for each directory.
29 * Pass 2 relies on the following information from previous passes:
30 * - The directory information collected in pass 1.
31 * - The inode_used_map bitmap
32 * - The inode_bad_map bitmap
33 * - The inode_dir_map bitmap
35 * Pass 2 frees the following data structures
36 * - The inode_bad_map bitmap
39 #include "et/com_err.h"
44 * Keeps track of how many times an inode is referenced.
46 unsigned short * inode_count;
48 static void deallocate_inode(ext2_filsys fs, ino_t ino,
50 static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino);
51 static int check_dir_block(ext2_filsys fs,
52 struct dir_block_struct *dir_blocks_info,
54 static int allocate_dir_block(ext2_filsys fs,
55 struct dir_block_struct *dir_blocks_info,
57 static int update_dir_block(ext2_filsys fs,
62 void pass2(ext2_filsys fs)
66 struct resource_track rtrack;
68 init_resource_track(&rtrack);
71 mtrace_print("Pass 2");
75 printf("Pass 2: Checking directory structure\n");
76 inode_count = allocate_memory((fs->super->s_inodes_count + 1) *
77 sizeof(unsigned short),
78 "buffer for inode count");
80 buf = allocate_memory(fs->blocksize, "directory scan buffer");
82 for (i=0; i < dir_block_count; i++)
83 check_dir_block(fs, &dir_blocks[i], buf);
88 ext2fs_free_inode_bitmap(inode_bad_map);
93 print_resource_track(&rtrack);
98 * Make sure the first entry in the directory is '.', and that the
99 * directory entry is sane.
101 static int check_dot(ext2_filsys fs,
102 struct ext2_dir_entry *dirent,
105 struct ext2_dir_entry *nextdir;
109 const char *question = 0;
111 if (!dirent->inode) {
112 printf("Missing '.' in directory inode %lu.\n", ino);
114 } else if ((dirent->name_len != 1) ||
115 strncmp(dirent->name, ".", dirent->name_len)) {
116 char *name = malloc(dirent->name_len + 1);
118 fatal_error("Couldn't allocate . name");
119 strncpy(name, dirent->name, dirent->name_len);
120 name[dirent->name_len] = '\0';
121 printf("First entry in directory inode %lu contains '%s' "
122 "(inode=%u)\n", ino, name, dirent->inode);
123 printf("instead of '.'.\n");
125 question = "Change to be '.'";
128 if (dirent->rec_len < 12)
129 fatal_error("Cannot fix, insufficient space to add '.'");
131 if (ask(question, 1)) {
133 dirent->name_len = 1;
134 dirent->name[0] = '.';
138 ext2fs_unmark_valid(fs);
142 if (dirent->inode != ino) {
143 printf("Bad inode number for '.' in directory inode %lu.\n",
150 ext2fs_unmark_valid(fs);
152 if (dirent->rec_len > 12) {
153 new_len = dirent->rec_len - 12;
157 ask("Directory entry for '.' is big. Split", 1)) {
158 nextdir = (struct ext2_dir_entry *)
159 ((char *) dirent + 12);
160 dirent->rec_len = 12;
161 nextdir->rec_len = new_len;
163 nextdir->name_len = 0;
172 * Make sure the second entry in the directory is '..', and that the
173 * directory entry is sane. We do not check the inode number of '..'
174 * here; this gets done in pass 3.
176 static int check_dotdot(ext2_filsys fs,
177 struct ext2_dir_entry *dirent,
178 struct dir_info *dir)
180 ino_t ino = dir->ino;
181 const char *question = 0;
183 if (!dirent->inode) {
184 printf("Missing '..' in directory inode %lu.\n", ino);
186 } else if ((dirent->name_len != 2) ||
187 strncmp(dirent->name, "..", dirent->name_len)) {
188 char *name = malloc(dirent->name_len + 1);
190 fatal_error("Couldn't allocate bad .. name");
191 strncpy(name, dirent->name, dirent->name_len);
192 name[dirent->name_len] = '\0';
193 printf("Second entry in directory inode %lu contains '%s' "
194 "(inode=%u)\n", ino, name, dirent->inode);
195 printf("instead of '..'.\n");
197 question = "Change to be '..'";
200 if (dirent->rec_len < 12)
201 fatal_error("Cannot fix, insufficient space to add '..'");
203 if (ask(question, 1)) {
205 * Note: we don't have the parent inode just
206 * yet, so we will fill it in with the root
207 * inode. This will get fixed in pass 3.
209 dirent->inode = EXT2_ROOT_INO;
210 dirent->name_len = 2;
211 dirent->name[0] = '.';
212 dirent->name[1] = '.';
215 ext2fs_unmark_valid(fs);
218 dir->dotdot = dirent->inode;
222 static char unknown_pathname[] = "???";
225 * Check to make sure a directory entry doesn't contain any illegal
228 static int check_name(ext2_filsys fs,
229 struct ext2_dir_entry *dirent,
239 for ( i = 0; i < dirent->name_len; i++) {
240 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
242 retval = ext2fs_get_pathname(fs, dir_ino,
245 com_err(program_name, retval, "while getting pathname in check_name");
246 pathname = unknown_pathname;
248 printf ("Bad file name '%s' (contains '/' or "
249 " null) in directory '%s' (%lu)\n",
250 name, pathname, dir_ino);
251 if (pathname != unknown_pathname)
254 fixup = ask("Replace '/' or null by '.'", 1);
257 dirent->name[i] = '.';
260 ext2fs_unmark_valid(fs);
266 static int check_dir_block(ext2_filsys fs,
267 struct dir_block_struct *db,
270 struct dir_info *subdir, *dir;
271 struct ext2_dir_entry *dirent;
273 int dir_modified = 0;
276 int dot_state, name_len;
277 blk_t block_nr = db->blk;
279 char name[EXT2_NAME_LEN+1];
282 * Make sure the inode is still in use (could have been
283 * deleted in the duplicate/bad blocks pass.
285 if (!(ext2fs_test_inode_bitmap(inode_used_map, ino)))
289 if (allocate_dir_block(fs, db, buf))
300 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
304 retval = ext2fs_read_dir_block(fs, block_nr, buf);
306 com_err(program_name, retval,
307 "while reading directory block %d", block_nr);
312 dirent = (struct ext2_dir_entry *) (buf + offset);
313 if (((offset + dirent->rec_len) > fs->blocksize) ||
314 (dirent->rec_len < 8) ||
315 ((dirent->name_len+8) > dirent->rec_len)) {
316 printf("Directory inode %lu, block %d, offset %d: directory corrupted\n",
317 ino, db->blockcnt, offset);
319 if (ask("Salvage", 1)) {
320 dirent->rec_len = fs->blocksize - offset;
321 dirent->name_len = 0;
325 ext2fs_unmark_valid(fs);
330 name_len = dirent->name_len;
331 if (dirent->name_len > EXT2_NAME_LEN) {
332 printf("Directory inode %lu, block %d, offset %d: filename too long\n",
333 ino, db->blockcnt, offset);
335 if (ask("Truncate filename", 1)) {
336 dirent->name_len = EXT2_NAME_LEN;
339 name_len = EXT2_NAME_LEN;
342 strncpy(name, dirent->name, name_len);
343 name[name_len] = '\0';
345 if (dot_state == 1) {
346 if (check_dot(fs, dirent, ino))
348 } else if (dot_state == 2) {
349 dir = get_dir_info(ino);
351 printf("Internal error: couldn't find dir_info for %lu\n",
355 if (check_dotdot(fs, dirent, dir))
357 } else if (dirent->inode == ino) {
358 retval = ext2fs_get_pathname(fs, ino, 0, &path1);
360 path1 = unknown_pathname;
361 printf("Entry '%s' in %s (%lu) is a link to '.' ",
363 if (path1 != unknown_pathname)
366 if (ask("Clear", 1)) {
375 printf("Entry '%s', name_len %d, rec_len %d, inode %lu... ",
376 name, dirent->name_len, dirent->rec_len, dirent->inode);
378 if (check_name(fs, dirent, ino, name))
382 * Make sure the inode listed is a legal one.
384 if (((dirent->inode != EXT2_ROOT_INO) &&
385 (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
386 (dirent->inode > fs->super->s_inodes_count)) {
387 retval = ext2fs_get_pathname(fs, ino, 0, &path1);
389 path1 = unknown_pathname;
390 printf("Entry '%s' in %s (%lu) has bad inode #: %u.\n",
391 name, path1, ino, dirent->inode);
392 if (path1 != unknown_pathname)
395 if (ask("Clear", 1)) {
400 ext2fs_unmark_valid(fs);
404 * If the inode is unusued, offer to clear it.
406 if (!(ext2fs_test_inode_bitmap(inode_used_map,
408 retval = ext2fs_get_pathname(fs, ino, 0, &path1);
410 path1 = unknown_pathname;
411 printf("Entry '%s' in %s (%lu) has deleted/unused inode %u.\n",
412 name, path1, ino, dirent->inode);
413 if (path1 != unknown_pathname)
415 if (ask("Clear", 1)) {
420 ext2fs_unmark_valid(fs);
424 * If the inode was marked as having bad fields in
425 * pass1, process it and offer to fix/clear it.
426 * (We wait until now so that we can display the
427 * pathname to the user.)
430 ext2fs_test_inode_bitmap(inode_bad_map,
432 if (process_bad_inode(fs, ino, dirent->inode)) {
440 * If this is a directory, then mark its parent in its
441 * dir_info structure. If the parent field is already
442 * filled in, then this directory has more than one
443 * hard link. We assume the first link is correct,
444 * and ask the user if he/she wants to clear this one.
446 if ((dot_state > 2) &&
447 (ext2fs_test_inode_bitmap(inode_dir_map,
449 subdir = get_dir_info(dirent->inode);
451 printf("INTERNAL ERROR: missing dir %u\n",
455 if (subdir->parent) {
456 retval = ext2fs_get_pathname(fs, ino,
459 path1 = unknown_pathname;
460 retval = ext2fs_get_pathname(fs,
465 path2 = unknown_pathname;
466 printf("Entry '%s' in %s (%lu) is a link to directory %s (%u).\n",
467 name, path1, ino, path2,
469 if (path1 != unknown_pathname)
471 if (path2 != unknown_pathname)
473 if (ask("Clear", 1)) {
478 ext2fs_unmark_valid(fs);
480 subdir->parent = ino;
483 if (inode_count[dirent->inode]++ > 0)
487 offset += dirent->rec_len;
488 } while (offset < fs->blocksize);
492 if (offset != fs->blocksize) {
493 printf("Final rec_len is %d, should be %d\n",
495 dirent->rec_len - fs->blocksize + offset);
498 retval = ext2fs_write_dir_block(fs, block_nr, buf);
500 com_err(program_name, retval,
501 "while writing directory block %d", block_nr);
503 ext2fs_mark_changed(fs);
509 * This function is called to deallocate a block, and is an interator
510 * functioned called by deallocate inode via ext2fs_iterate_block().
512 static int deallocate_inode_block(ext2_filsys fs,
519 ext2fs_unmark_block_bitmap(block_found_map, *block_nr);
520 ext2fs_unmark_block_bitmap(fs->block_map, *block_nr);
525 * This fuction deallocates an inode
527 static void deallocate_inode(ext2_filsys fs, ino_t ino,
531 struct ext2_inode inode;
533 inode_link_info[ino] = 0;
534 e2fsck_read_inode(fs, ino, &inode, "deallocate_inode");
535 inode.i_links_count = 0;
536 inode.i_dtime = time(0);
537 e2fsck_write_inode(fs, ino, &inode, "deallocate_inode");
540 * Fix up the bitmaps...
543 ext2fs_unmark_inode_bitmap(inode_used_map, ino);
544 ext2fs_unmark_inode_bitmap(inode_dir_map, ino);
546 ext2fs_unmark_inode_bitmap(inode_bad_map, ino);
547 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
548 ext2fs_mark_ib_dirty(fs);
550 if (!inode_has_valid_blocks(&inode))
553 ext2fs_mark_bb_dirty(fs);
554 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
555 deallocate_inode_block, 0);
557 com_err("deallocate_inode", retval,
558 "while calling ext2fs_block_iterate for inode %d",
563 * These two subroutines are used by process_bad_inode; it is used to
564 * make sure that certain reserved fields are really zero. If not,
565 * prompt the user if he/she wants us to zeroize them.
567 static void check_for_zero_u32(ext2_filsys fs, ino_t ino, char *pathname,
568 const char *name, __u32 *val,
574 printf("%s for inode %lu (%s) is %u, should be zero.\n",
575 name, ino, pathname, *val);
577 sprintf(prompt, "Clear %s", name);
578 if (ask(prompt, 1)) {
582 ext2fs_unmark_valid(fs);
586 static void check_for_zero_u8(ext2_filsys fs, ino_t ino, char *pathname,
587 const char *name, __u8 *val,
593 printf("%s for inode %lu (%s) is %d, should be zero.\n",
594 name, ino, pathname, *val);
596 sprintf(prompt, "Clear %s", name);
597 if (ask(prompt, 1)) {
601 ext2fs_unmark_valid(fs);
607 static int process_bad_inode(ext2_filsys fs, ino_t dir, ino_t ino)
609 struct ext2_inode inode;
611 int inode_modified = 0;
613 unsigned char *frag, *fsize;
615 e2fsck_read_inode(fs, ino, &inode, "process_bad_inode");
616 retval = ext2fs_get_pathname(fs, dir, ino, &pathname);
618 com_err("process_bad_inode", retval,
619 "while getting pathname for inode %d",
623 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
624 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
625 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
626 !(LINUX_S_ISSOCK(inode.i_mode))) {
627 printf("Inode %lu (%s) has a bad mode (0%o).\n",
628 ino, pathname, inode.i_mode);
630 if (ask("Clear", 1)) {
631 deallocate_inode(fs, ino, 0);
635 ext2fs_unmark_valid(fs);
637 check_for_zero_u32(fs, ino, pathname, "i_faddr", &inode.i_faddr,
640 switch (fs->super->s_creator_os) {
642 frag = &inode.osd2.linux2.l_i_frag;
643 fsize = &inode.osd2.linux2.l_i_fsize;
646 frag = &inode.osd2.hurd2.h_i_frag;
647 fsize = &inode.osd2.hurd2.h_i_fsize;
650 frag = &inode.osd2.masix2.m_i_frag;
651 fsize = &inode.osd2.masix2.m_i_fsize;
657 check_for_zero_u8(fs, ino, pathname, "i_frag", frag,
660 check_for_zero_u8(fs, ino, pathname, "i_fsize", fsize,
663 check_for_zero_u32(fs, ino, pathname, "i_file_acl", &inode.i_file_acl,
665 check_for_zero_u32(fs, ino, pathname, "i_dir_acl", &inode.i_dir_acl,
669 e2fsck_write_inode(fs, ino, &inode, "process_bad_inode");
675 * allocate_dir_block --- this function allocates a new directory
676 * block for a particular inode; this is done if a directory has
677 * a "hole" in it, or if a directory has a illegal block number
678 * that was zeroed out and now needs to be replaced.
680 static int allocate_dir_block(ext2_filsys fs,
681 struct dir_block_struct *db,
686 struct ext2_inode inode;
689 printf("Directory inode %lu has a hole at block #%d\n",
690 db->ino, db->blockcnt);
691 if (ask("Allocate block", 1) == 0)
695 * Read the inode and block bitmaps in; we'll be messing with
701 * First, find a free block
703 retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
705 com_err("allocate_dir_block", retval,
706 "while trying to fill a hole in a directory inode");
709 ext2fs_mark_block_bitmap(block_found_map, blk);
710 ext2fs_mark_block_bitmap(fs->block_map, blk);
711 ext2fs_mark_bb_dirty(fs);
714 * Now let's create the actual data block for the inode
717 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
719 retval = ext2fs_new_dir_block(fs, db->ino, EXT2_ROOT_INO,
723 com_err("allocate_dir_block", retval,
724 "while creating new directory block");
728 retval = ext2fs_write_dir_block(fs, blk, block);
731 com_err("allocate_dir_block", retval,
732 "while writing an empty directory block");
737 * Update the inode block count
739 e2fsck_read_inode(fs, db->ino, &inode, "allocate_dir_block");
740 inode.i_blocks += fs->blocksize / 512;
741 if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
742 inode.i_size = (db->blockcnt+1) * fs->blocksize;
743 e2fsck_write_inode(fs, db->ino, &inode, "allocate_dir_block");
746 * Finally, update the block pointers for the inode
749 retval = ext2fs_block_iterate(fs, db->ino, BLOCK_FLAG_HOLE,
750 0, update_dir_block, db);
752 com_err("allocate_dir_block", retval,
753 "while calling ext2fs_block_iterate");
761 * This is a helper function for allocate_dir_block().
763 static int update_dir_block(ext2_filsys fs,
768 struct dir_block_struct *db = private;
770 if (db->blockcnt == blockcnt) {
772 return BLOCK_CHANGED;