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.
36 #include "et/com_err.h"
40 static void check_root(ext2_filsys fs, ino_t root_ino);
41 static void check_directory(ext2_filsys fs, ino_t dir);
42 static ino_t get_lost_and_found(ext2_filsys fs);
43 static void fix_dotdot(ext2_filsys fs, struct dir_info *dir, ino_t parent);
44 static errcode_t adjust_inode_count(ext2_filsys fs, ino_t ino, int adj);
45 static errcode_t expand_directory(ext2_filsys fs, ino_t dir);
47 static ino_t lost_and_found = 0;
48 static int bad_lost_and_found = 0;
50 static ext2fs_inode_bitmap inode_loop_detect;
51 static ext2fs_inode_bitmap inode_done_map;
53 void pass3(ext2_filsys fs)
57 struct resource_track rtrack;
59 init_resource_track(&rtrack);
62 mtrace_print("Pass 3");
66 printf("Pass 3: Checking directory connectivity\n");
69 * Allocate some bitmaps to do loop detection.
71 retval = ext2fs_allocate_inode_bitmap(fs,
72 "inode loop detection bitmap",
75 com_err("ext2fs_allocate_inode_bitmap", retval,
76 "while allocating inode_loop_detect");
79 retval = ext2fs_allocate_inode_bitmap(fs, "inode done bitmap",
82 com_err("ext2fs_allocate_inode_bitmap", retval,
83 "while allocating inode_done_map");
87 printf("Peak memory: ");
88 print_resource_track(&global_rtrack);
91 check_root(fs, EXT2_ROOT_INO);
92 ext2fs_mark_inode_bitmap(inode_done_map, EXT2_ROOT_INO);
94 for (i=1; i <= fs->super->s_inodes_count; i++) {
95 if (ext2fs_test_inode_bitmap(inode_dir_map, i))
96 check_directory(fs, i);
100 ext2fs_free_inode_bitmap(inode_loop_detect);
101 ext2fs_free_inode_bitmap(inode_done_map);
104 print_resource_track(&rtrack);
109 * This makes sure the root inode is present; if not, we ask if the
110 * user wants us to create it. Not creating it is a fatal error.
112 void check_root(ext2_filsys fs, ino_t root_ino)
116 struct ext2_inode inode;
118 struct dir_info *dir;
120 if (ext2fs_test_inode_bitmap(inode_used_map, root_ino)) {
122 * If the root inode is a directory, die here. The
123 * user must have answered 'no' in pass1 when we
124 * offered to clear it.
126 if (!(ext2fs_test_inode_bitmap(inode_dir_map, root_ino)))
127 fatal_error("Root inode not directory");
130 * Set up the parent pointer for the root; this isn't
131 * done anywhere else, so we do it here.
133 dir = get_dir_info(root_ino);
134 dir->parent = root_ino;
139 printf("Root inode not allocated. ");
141 if (!ask("Reallocate", 1)) {
142 ext2fs_unmark_valid(fs);
143 fatal_error("Cannot proceed without a root inode.");
149 * First, find a free block
151 retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
153 com_err("ext2fs_new_block", retval,
154 "while trying to create root directory");
157 ext2fs_mark_block_bitmap(block_found_map, blk);
158 ext2fs_mark_block_bitmap(fs->block_map, blk);
159 ext2fs_mark_bb_dirty(fs);
162 * Now let's create the actual data block for the inode
164 retval = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
167 com_err("ext2fs_new_dir_block", retval,
168 "while creating new root directory");
172 retval = ext2fs_write_dir_block(fs, blk, block);
174 com_err("ext2fs_write_dir_block", retval,
175 "while writing the root directory block");
181 * Set up the inode structure
183 memset(&inode, 0, sizeof(inode));
184 inode.i_mode = 040755;
185 inode.i_size = fs->blocksize;
186 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
187 inode.i_links_count = 2;
188 inode.i_blocks = fs->blocksize / 512;
189 inode.i_block[0] = blk;
192 * Write out the inode.
194 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
196 com_err("ext2fs_write_inode", retval,
197 "While trying to create /lost+found");
202 * Miscellaneous bookkeeping...
204 add_dir_info(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, &inode);
205 inode_count[EXT2_ROOT_INO] = 2;
206 inode_link_info[EXT2_ROOT_INO] = 2;
208 ext2fs_mark_inode_bitmap(inode_used_map, EXT2_ROOT_INO);
209 ext2fs_mark_inode_bitmap(inode_dir_map, EXT2_ROOT_INO);
210 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO);
211 ext2fs_mark_ib_dirty(fs);
215 * This subroutine is responsible for making sure that a particular
216 * directory is connected to the root; if it isn't we trace it up as
217 * far as we can go, and then offer to connect the resulting parent to
218 * the lost+found. We have to do loop detection; if we ever discover
219 * a loop, we treat that as a disconnected directory and offer to
220 * reparent it to lost+found.
222 static void check_directory(ext2_filsys fs, ino_t ino)
224 struct dir_info *dir;
227 char *path1, *path2, *path3;
228 static char unknown[] = "???";
230 dir = get_dir_info(ino);
232 printf("Internal error: couldn't find dir_info for %lu\n",
237 ext2fs_clear_inode_bitmap(inode_loop_detect);
241 * If we find a parent which we've already checked,
242 * then stop; we know it's either already connected to
243 * the directory tree, or it isn't but the user has
244 * already told us he doesn't want us to reconnect the
245 * disconnected subtree.
247 if (ext2fs_test_inode_bitmap(inode_done_map, p->ino))
250 * Mark this inode as being "done"; by the time we
251 * return from this function, the inode we either be
252 * verified as being connected to the directory tree,
253 * or we will have offered to reconnect this to
256 ext2fs_mark_inode_bitmap(inode_done_map, p->ino);
258 * If this directory doesn't have a parent, or we've
259 * seen the parent once already, then offer to
260 * reparent it to lost+found
263 (ext2fs_test_inode_bitmap(inode_loop_detect,
266 ext2fs_mark_inode_bitmap(inode_loop_detect,
268 p = get_dir_info(p->parent);
271 * If we've reached here, we've hit a detached directory
272 * inode; offer to reconnect it to lost+found.
274 retval = ext2fs_get_pathname(fs, p->ino, 0, &path1);
278 printf("Unconnected directory inode %lu (%s)\n", p->ino, path1);
279 if (path1 != unknown)
282 if (ask("Connect to /lost+found", 1)) {
283 if (reconnect_file(fs, p->ino))
284 ext2fs_unmark_valid(fs);
286 p->parent = lost_and_found;
287 fix_dotdot(fs, p, lost_and_found);
291 ext2fs_unmark_valid(fs);
294 * Make sure that .. and the parent directory are the same;
295 * offer to fix it if not.
298 if (dir->parent != dir->dotdot) {
299 retval = ext2fs_get_pathname(fs, dir->parent, ino,
303 retval = ext2fs_get_pathname(fs, dir->dotdot, 0, &path2);
306 retval = ext2fs_get_pathname(fs, dir->parent, 0, &path3);
310 printf("'..' in %s (%lu) is %s (%lu), should be %s (%lu).\n",
311 path1, ino, path2, dir->dotdot,
313 if (path1 != unknown)
315 if (path2 != unknown)
317 if (path3 != unknown)
320 fix_dotdot(fs, dir, dir->parent);
322 ext2fs_unmark_valid(fs);
327 * This routine gets the lost_and_found inode, making it a directory
330 ino_t get_lost_and_found(ext2_filsys fs)
335 struct ext2_inode inode;
337 const char * name = "lost+found";
339 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
342 if (retval != ENOENT)
343 printf("Error while trying to find /lost+found: %s",
344 error_message(retval));
346 printf("/lost+found not found. ");
348 if (!ask("Create", 1)) {
349 ext2fs_unmark_valid(fs);
354 * Read the inode and block bitmaps in; we'll be messing with
360 * First, find a free block
362 retval = ext2fs_new_block(fs, 0, block_found_map, &blk);
364 com_err("ext2fs_new_block", retval,
365 "while trying to create /lost+found directory");
368 ext2fs_mark_block_bitmap(block_found_map, blk);
369 ext2fs_mark_block_bitmap(fs->block_map, blk);
370 ext2fs_mark_bb_dirty(fs);
373 * Next find a free inode.
375 retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040755, inode_used_map,
378 com_err("ext2fs_new_inode", retval,
379 "while trying to create /lost+found directory");
382 ext2fs_mark_inode_bitmap(inode_used_map, ino);
383 ext2fs_mark_inode_bitmap(inode_dir_map, ino);
384 ext2fs_mark_inode_bitmap(fs->inode_map, ino);
385 ext2fs_mark_ib_dirty(fs);
388 * Now let's create the actual data block for the inode
390 retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
392 com_err("ext2fs_new_dir_block", retval,
393 "while creating new directory block");
397 retval = ext2fs_write_dir_block(fs, blk, block);
400 com_err("ext2fs_write_dir_block", retval,
401 "while writing the directory block for /lost+found");
406 * Set up the inode structure
408 memset(&inode, 0, sizeof(inode));
409 inode.i_mode = 040755;
410 inode.i_size = fs->blocksize;
411 inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
412 inode.i_links_count = 2;
413 inode.i_blocks = fs->blocksize / 512;
414 inode.i_block[0] = blk;
417 * Next, write out the inode.
419 retval = ext2fs_write_inode(fs, ino, &inode);
421 com_err("ext2fs_write_inode", retval,
422 "While trying to create /lost+found");
426 * Finally, create the directory link
428 retval = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, 0);
430 com_err("ext2fs_link", retval, "While creating /lost+found");
435 * Miscellaneous bookkeeping that needs to be kept straight.
437 add_dir_info(fs, ino, EXT2_ROOT_INO, &inode);
438 adjust_inode_count(fs, EXT2_ROOT_INO, +1);
439 inode_count[ino] = 2;
440 inode_link_info[ino] = 2;
442 printf("/lost+found created; inode #%lu\n", ino);
448 * This routine will connect a file to lost+found
450 int reconnect_file(ext2_filsys fs, ino_t inode)
455 if (bad_lost_and_found) {
456 printf("Bad or nonexistent /lost+found. Cannot reconnect.\n");
459 if (!lost_and_found) {
460 lost_and_found = get_lost_and_found(fs);
461 if (!lost_and_found) {
462 printf("Bad or nonexistent /lost+found. Cannot reconnect.\n");
463 bad_lost_and_found++;
468 sprintf(name, "#%lu", inode);
469 retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
470 if (retval == EXT2_ET_DIR_NO_SPACE) {
471 if (!ask("No room in /lost+found; expand /lost+found", 1))
473 retval = expand_directory(fs, lost_and_found);
475 printf("Could not expand /lost+found: %s\n",
476 error_message(retval));
479 retval = ext2fs_link(fs, lost_and_found, name, inode, 0);
482 printf("Could not reconnect %lu: %s\n", inode,
483 error_message(retval));
487 adjust_inode_count(fs, inode, +1);
493 * Utility routine to adjust the inode counts on an inode.
495 static errcode_t adjust_inode_count(ext2_filsys fs, ino_t ino, int adj)
498 struct ext2_inode inode;
503 retval = ext2fs_read_inode(fs, ino, &inode);
508 printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
509 inode.i_links_count);
512 inode.i_links_count += adj;
513 inode_count[ino] += adj;
514 inode_link_info[ino] += adj;
516 retval = ext2fs_write_inode(fs, ino, &inode);
524 * Fix parent --- this routine fixes up the parent of a directory.
526 struct fix_dotdot_struct {
532 static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
538 struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) private;
541 if (dirent->name_len != 2)
543 if (strncmp(dirent->name, "..", 2))
546 retval = adjust_inode_count(fp->fs, dirent->inode, -1);
548 printf("Error while adjusting inode count on inode %u\n",
550 retval = adjust_inode_count(fp->fs, fp->parent, 1);
552 printf("Error while adjusting inode count on inode %lu\n",
555 dirent->inode = fp->parent;
558 return DIRENT_ABORT | DIRENT_CHANGED;
561 static void fix_dotdot(ext2_filsys fs, struct dir_info *dir, ino_t parent)
564 struct fix_dotdot_struct fp;
571 printf("Fixing '..' of inode %lu to be %lu...\n", dir->ino, parent);
574 retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY,
575 0, fix_dotdot_proc, &fp);
576 if (retval || !fp.done) {
577 printf("Couldn't fix parent of inode %lu: %s\n\n",
578 dir->ino, retval ? error_message(retval) :
579 "Couldn't find parent direntory entry");
580 ext2fs_unmark_valid(fs);
582 dir->dotdot = parent;
588 * These routines are responsible for expanding a /lost+found if it is
592 struct expand_dir_struct {
597 static int expand_dir_proc(ext2_filsys fs,
602 struct expand_dir_struct *es = (struct expand_dir_struct *) private;
604 static blk_t last_blk = 0;
612 retval = ext2fs_new_block(fs, last_blk, block_found_map, &new_blk);
618 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
625 block = malloc(fs->blocksize);
630 memset(block, 0, fs->blocksize);
632 retval = ext2fs_write_dir_block(fs, new_blk, block);
639 ext2fs_mark_block_bitmap(block_found_map, new_blk);
640 ext2fs_mark_block_bitmap(fs->block_map, new_blk);
641 ext2fs_mark_bb_dirty(fs);
643 return (BLOCK_CHANGED | BLOCK_ABORT);
645 return BLOCK_CHANGED;
648 static errcode_t expand_directory(ext2_filsys fs, ino_t dir)
651 struct expand_dir_struct es;
652 struct ext2_inode inode;
654 if (!(fs->flags & EXT2_FLAG_RW))
655 return EXT2_ET_RO_FILSYS;
657 retval = ext2fs_check_directory(fs, dir);
664 retval = ext2fs_block_iterate(fs, dir, BLOCK_FLAG_APPEND,
665 0, expand_dir_proc, &es);
670 return EXT2_ET_EXPAND_DIR_ERR;
673 * Update the size and block count fields in the inode.
675 retval = ext2fs_read_inode(fs, dir, &inode);
679 inode.i_size += fs->blocksize;
680 inode.i_blocks += fs->blocksize / 512;
682 e2fsck_write_inode(fs, dir, &inode, "expand_directory");