2 * pass3.c -- pass #3 of e2fsck: Check for directory connectivity
4 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
7 * Pass #3 assures that all directories are connected to the
8 * filesystem tree, using the following algorithm:
10 * First, the root directory is checked to make sure it exists; if
11 * not, e2fsck will offer to create a new one. It is then marked as
14 * Then, pass3 interates over all directory inodes; for each directory
15 * it attempts to trace up the filesystem tree, using dirinfo.parent
16 * until it reaches a directory which has been marked "done". If it
17 * can not do so, then the directory must be disconnected, and e2fsck
18 * will offer to reconnect it to /lost+found. While it is chasing
19 * parent pointers up the filesystem tree, if pass3 sees a directory
20 * twice, then it has detected a filesystem loop, and it will again
21 * offer to reconnect the directory to /lost+found in to break the
24 * Pass 3 also contains the subroutine, reconnect_file() to reconnect
25 * inodes to /lost+found; this subroutine is also used by pass 4.
26 * reconnect_file() calls get_lost_and_found(), which is responsible
27 * for creating /lost+found if it does not exist.
29 * Pass 3 frees the following data structures:
30 * - The dirinfo directory information cache.
33 #include "et/com_err.h"
37 static void check_root(ext2_filsys fs, ino_t root_ino);
38 static void check_directory(ext2_filsys fs, ino_t dir);
39 static ino_t get_lost_and_found(ext2_filsys fs);
40 static void fix_dotdot(ext2_filsys fs, struct dir_info *dir, ino_t parent);
41 static int adjust_inode_count(ext2_filsys fs, ino_t ino, int adj);
42 static errcode_t expand_directory(ext2_filsys fs, ino_t dir);
44 static ino_t lost_and_found = 0;
45 static int bad_lost_and_found = 0;
47 static char *inode_loop_detect;
48 static char *inode_done_map;
50 void pass3(ext2_filsys fs)
54 struct resource_track rtrack;
56 init_resource_track(&rtrack);
59 mtrace_print("Pass 3");
63 printf("Pass 3: Checking directory connectivity\n");
66 * Allocate some bitmaps to do loop detection.
68 retval = ext2fs_allocate_inode_bitmap(fs, &inode_loop_detect);
70 com_err("ext2fs_allocate_inode_bitmap", retval,
71 "while allocating inode_loop_detect");
74 retval = ext2fs_allocate_inode_bitmap(fs, &inode_done_map);
76 com_err("ext2fs_allocate_inode_bitmap", retval,
77 "while allocating inode_done_map");
81 printf("Peak memory: ");
82 print_resource_track(&global_rtrack);
85 check_root(fs, EXT2_ROOT_INO);
86 ext2fs_mark_inode_bitmap(fs, inode_done_map, EXT2_ROOT_INO);
88 for (i=1; i <= fs->super->s_inodes_count; i++) {
89 if (ext2fs_test_inode_bitmap(fs, inode_dir_map, i))
90 check_directory(fs, i);
94 free(inode_loop_detect);
98 print_resource_track(&rtrack);
103 * This makes sure the root inode is present; if not, we ask if the
104 * user wants us to create it. Not creating it is a fatal error.
106 void check_root(ext2_filsys fs, ino_t root_ino)
110 struct ext2_inode inode;
112 struct dir_info *dir;
114 if (ext2fs_test_inode_bitmap(fs, inode_used_map, root_ino)) {
116 * If the root inode is a directory, die here. The
117 * user must have answered 'no' in pass1 when we
118 * offered to clear it.
120 if (!(ext2fs_test_inode_bitmap(fs, inode_dir_map, root_ino)))
121 fatal_error("Root inode not directory");
124 * Set up the parent pointer for the root; this isn't
125 * done anywhere else, so we do it here.
127 dir = get_dir_info(root_ino);
128 dir->parent = root_ino;
133 printf("Root inode not allocated. ");
135 if (!ask("Rellocate", 1)) {
136 ext2fs_unmark_valid(fs);
137 fatal_error("Cannot proceed without a root inode.");
143 * First, find a free block
145 retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
147 com_err("ext2fs_new_block", retval,
148 "while trying to create root directory");
151 ext2fs_mark_block_bitmap(fs, block_found_map, blk);
152 ext2fs_mark_block_bitmap(fs, fs->block_map, blk);
153 ext2fs_mark_bb_dirty(fs);
156 * Now let's create the actual data block for the inode
158 retval = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
161 com_err("ext2fs_new_dir_block", retval,
162 "while creating new root directory");
166 retval = io_channel_write_blk(fs->io, blk, 1, block);
168 com_err("io_channel_write_blk", retval,
169 "while writing the root directory block");
175 * Set up the inode structure
177 memset(&inode, 0, sizeof(inode));
178 inode.i_mode = 040755;
179 inode.i_size = fs->blocksize;
180 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
181 inode.i_links_count = 2;
182 inode.i_blocks = fs->blocksize / 512;
183 inode.i_block[0] = blk;
186 * Write out the inode.
188 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
190 com_err("ext2fs_write_inode", retval,
191 "While trying to create /lost+found");
196 * Miscellaneous bookkeeping...
198 add_dir_info(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, &inode);
199 inode_count[EXT2_ROOT_INO] = 2;
200 inode_link_info[EXT2_ROOT_INO] = 2;
202 ext2fs_mark_inode_bitmap(fs, inode_used_map, EXT2_ROOT_INO);
203 ext2fs_mark_inode_bitmap(fs, inode_dir_map, EXT2_ROOT_INO);
204 ext2fs_mark_inode_bitmap(fs, fs->inode_map, EXT2_ROOT_INO);
205 ext2fs_mark_ib_dirty(fs);
209 * This subroutine is responsible for making sure that a particular
210 * directory is connected to the root; if it isn't we trace it up as
211 * far as we can go, and then offer to connect the resulting parent to
212 * the lost+found. We have to do loop detection; if we ever discover
213 * a loop, we treat that as a disconnected directory and offer to
214 * reparent it to lost+found.
216 static void check_directory(ext2_filsys fs, ino_t ino)
218 struct dir_info *dir;
221 char *path1, *path2, *path3;
222 static char unknown[] = "???";
224 dir = get_dir_info(ino);
226 printf("Internal error: couldn't find dir_info for %ld\n",
231 memset(inode_loop_detect, 0, (fs->super->s_inodes_count / 8) + 1);
235 * If we find a parent which we've already checked,
236 * then stop; we know it's either already connected to
237 * the directory tree, or it isn't but the user has
238 * already told us he doesn't want us to reconnect the
239 * disconnected subtree.
241 if (ext2fs_test_inode_bitmap(fs, inode_done_map, p->ino))
244 * Mark this inode as being "done"; by the time we
245 * return from this function, the inode we either be
246 * verified as being connected to the directory tree,
247 * or we will have offered to reconnect this to
250 ext2fs_mark_inode_bitmap(fs, inode_done_map, p->ino);
252 * If this directory doesn't have a parent, or we've
253 * seen the parent once already, then offer to
254 * reparent it to lost+found
257 (ext2fs_test_inode_bitmap(fs, inode_loop_detect,
260 ext2fs_mark_inode_bitmap(fs, inode_loop_detect,
262 p = get_dir_info(p->parent);
265 * If we've reached here, we've hit a detached directory
266 * inode; offer to reconnect it to lost+found.
268 retval = ext2fs_get_pathname(fs, p->ino, 0, &path1);
272 printf("Unconnected directory inode %li (%s)\n", p->ino, path1);
273 if (path1 != unknown)
276 if (ask("Connect to /lost+found", 1)) {
277 if (reconnect_file(fs, p->ino))
278 ext2fs_unmark_valid(fs);
280 p->parent = lost_and_found;
281 fix_dotdot(fs, p, lost_and_found);
285 ext2fs_unmark_valid(fs);
288 * Make sure that .. and the parent directory are the same;
289 * offer to fix it if not.
292 if (dir->parent != dir->dotdot) {
293 retval = ext2fs_get_pathname(fs, dir->parent, ino,
297 retval = ext2fs_get_pathname(fs, dir->dotdot, 0, &path2);
300 retval = ext2fs_get_pathname(fs, dir->parent, 0, &path3);
304 printf("'..' in %s (%ld) is %s (%ld), should be %s (%ld).\n",
305 path1, ino, path2, dir->dotdot,
307 if (path1 != unknown)
309 if (path2 != unknown)
311 if (path3 != unknown)
314 fix_dotdot(fs, dir, dir->parent);
316 ext2fs_unmark_valid(fs);
321 * This routine gets the lost_and_found inode, making it a directory
324 ino_t get_lost_and_found(ext2_filsys fs)
329 struct ext2_inode inode;
331 const char * name = "lost+found";
333 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
336 if (retval != ENOENT)
337 printf("Error while trying to find /lost+found: %s",
338 error_message(retval));
340 printf("/lost+found not found. ");
342 if (!ask("Create", 1)) {
343 ext2fs_unmark_valid(fs);
348 * Read the inode and block bitmaps in; we'll be messing with
354 * First, find a free block
356 retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
358 com_err("ext2fs_new_block", retval,
359 "while trying to create /lost+found directory");
362 ext2fs_mark_block_bitmap(fs, block_found_map, blk);
363 ext2fs_mark_block_bitmap(fs, fs->block_map, blk);
364 ext2fs_mark_bb_dirty(fs);
367 * Next find a free inode.
369 retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040755, inode_used_map,
372 com_err("ext2fs_new_inode", retval,
373 "while trying to create /lost+found directory");
376 ext2fs_mark_inode_bitmap(fs, inode_used_map, ino);
377 ext2fs_mark_inode_bitmap(fs, inode_dir_map, ino);
378 ext2fs_mark_inode_bitmap(fs, fs->inode_map, ino);
379 ext2fs_mark_ib_dirty(fs);
382 * Now let's create the actual data block for the inode
384 retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
386 com_err("ext2fs_new_dir_block", retval,
387 "while creating new directory block");
391 retval = io_channel_write_blk(fs->io, blk, 1, block);
393 com_err("io_channel_write_blk", retval,
394 "while writing the directory block for /lost+found");
400 * Set up the inode structure
402 memset(&inode, 0, sizeof(inode));
403 inode.i_mode = 040755;
404 inode.i_size = fs->blocksize;
405 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
406 inode.i_links_count = 2;
407 inode.i_blocks = fs->blocksize / 512;
408 inode.i_block[0] = blk;
411 * Next, write out the inode.
413 retval = ext2fs_write_inode(fs, ino, &inode);
415 com_err("ext2fs_write_inode", retval,
416 "While trying to create /lost+found");
420 * Finally, create the directory link
422 retval = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, 0);
424 com_err("ext2fs_link", retval, "While creating /lost+found");
429 * Miscellaneous bookkeeping that needs to be kept straight.
431 add_dir_info(fs, ino, EXT2_ROOT_INO, &inode);
432 adjust_inode_count(fs, EXT2_ROOT_INO, +1);
433 inode_count[ino] = 2;
434 inode_link_info[ino] = 2;
436 printf("/lost+found created; inode #%d\n", ino);
442 * This routine will connect a file to lost+found
444 int reconnect_file(ext2_filsys fs, ino_t inode)
449 if (bad_lost_and_found) {
450 printf("Bad or nonexistent /lost+found. Cannot reconnect.\n");
453 if (!lost_and_found) {
454 lost_and_found = get_lost_and_found(fs);
455 if (!lost_and_found) {
456 printf("Bad or nonexistent /lost+found. Cannot reconnect.\n");
457 bad_lost_and_found++;
462 sprintf(name, "#%ld", inode);
463 retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
464 if (retval == EXT2_ET_DIR_NO_SPACE) {
465 if (!ask("No room in /lost+found; expand /lost+found", 1))
467 retval = expand_directory(fs, lost_and_found);
469 printf("Could not expand /lost+found: %s\n",
470 error_message(retval));
473 retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
476 printf("Could not reconnect %ld: %s\n", inode,
477 error_message(retval));
481 adjust_inode_count(fs, inode, +1);
487 * Utility routine to adjust the inode counts on an inode.
489 static int adjust_inode_count(ext2_filsys fs, ino_t ino, int adj)
492 struct ext2_inode inode;
497 retval = ext2fs_read_inode(fs, ino, &inode);
502 printf("Adjusting link count for inode %d by %d (from %d)\n", ino, adj,
503 inode.i_links_count);
506 inode.i_links_count += adj;
507 inode_count[ino] += adj;
508 inode_link_info[ino] += adj;
510 retval = ext2fs_write_inode(fs, ino, &inode);
518 * Fix parent --- this routine fixes up the parent of a directory.
520 struct fix_dotdot_struct {
526 static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
532 struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) private;
535 if (dirent->name_len != 2)
537 if (strncmp(dirent->name, "..", 2))
540 retval = adjust_inode_count(fp->fs, dirent->inode, -1);
542 printf("Error while adjusting inode count on inode %ld\n",
544 retval = adjust_inode_count(fp->fs, fp->parent, 1);
546 printf("Error while adjusting inode count on inode %ld\n",
549 dirent->inode = fp->parent;
552 return DIRENT_ABORT | DIRENT_CHANGED;
555 static void fix_dotdot(ext2_filsys fs, struct dir_info *dir, ino_t parent)
558 struct fix_dotdot_struct fp;
565 printf("Fixing '..' of inode %d to be %d...\n", dir->ino, parent);
568 retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY,
569 0, fix_dotdot_proc, &fp);
570 if (retval || !fp.done) {
571 printf("Couldn't fix parent of inode %ld: %s\n\n",
572 dir->ino, retval ? error_message(retval) :
573 "Couldn't find parent direntory entry");
574 ext2fs_unmark_valid(fs);
576 dir->dotdot = parent;
582 * These routines are responsible for expanding a /lost+found if it is
586 struct expand_dir_struct {
591 static int expand_dir_proc(ext2_filsys fs,
596 struct expand_dir_struct *es = (struct expand_dir_struct *) private;
598 static blk_t last_blk = 0;
606 retval = ext2fs_new_block(fs, last_blk, block_found_map, &new_blk);
612 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
619 block = malloc(fs->blocksize);
624 memset(block, 0, fs->blocksize);
626 retval = io_channel_write_blk(fs->io, new_blk, 1, block);
633 ext2fs_mark_block_bitmap(fs, block_found_map, new_blk);
634 ext2fs_mark_block_bitmap(fs, fs->block_map, new_blk);
635 ext2fs_mark_bb_dirty(fs);
637 return (BLOCK_CHANGED | BLOCK_ABORT);
639 return BLOCK_CHANGED;
642 static errcode_t expand_directory(ext2_filsys fs, ino_t dir)
645 struct expand_dir_struct es;
646 struct ext2_inode inode;
648 if (!(fs->flags & EXT2_FLAG_RW))
649 return EXT2_ET_RO_FILSYS;
651 retval = ext2fs_check_directory(fs, dir);
658 retval = ext2fs_block_iterate(fs, dir, BLOCK_FLAG_APPEND,
659 0, expand_dir_proc, &es);
664 return EXT2_ET_EXPAND_DIR_ERR;
667 * Update the size and block count fields in the inode.
669 retval = ext2fs_read_inode(fs, dir, &inode);
673 inode.i_size += fs->blocksize;
674 inode.i_blocks += fs->blocksize / 512;
676 retval = ext2fs_write_inode(fs, dir, &inode);