2 * pass3.c -- pass #3 of e2fsck: Check for directory connectivity
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
11 * Pass #3 assures that all directories are connected to the
12 * filesystem tree, using the following algorithm:
14 * First, the root directory is checked to make sure it exists; if
15 * not, e2fsck will offer to create a new one. It is then marked as
18 * Then, pass3 interates over all directory inodes; for each directory
19 * it attempts to trace up the filesystem tree, using dirinfo.parent
20 * until it reaches a directory which has been marked "done". If it
21 * can not do so, then the directory must be disconnected, and e2fsck
22 * will offer to reconnect it to /lost+found. While it is chasing
23 * parent pointers up the filesystem tree, if pass3 sees a directory
24 * twice, then it has detected a filesystem loop, and it will again
25 * offer to reconnect the directory to /lost+found in to break the
28 * Pass 3 also contains the subroutine, e2fsck_reconnect_file() to
29 * reconnect inodes to /lost+found; this subroutine is also used by
30 * pass 4. e2fsck_reconnect_file() calls get_lost_and_found(), which
31 * is responsible for creating /lost+found if it does not exist.
33 * Pass 3 frees the following data structures:
34 * - The dirinfo directory information cache.
44 static void check_root(e2fsck_t ctx);
45 static int check_directory(e2fsck_t ctx, struct dir_info *dir,
46 struct problem_context *pctx);
47 static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent);
49 static ext2fs_inode_bitmap inode_loop_detect = 0;
50 static ext2fs_inode_bitmap inode_done_map = 0;
52 void e2fsck_pass3(e2fsck_t ctx)
54 ext2_filsys fs = ctx->fs;
57 struct resource_track rtrack;
59 struct problem_context pctx;
61 unsigned long maxdirs, count;
64 init_resource_track(&rtrack);
67 clear_problem_context(&pctx);
70 mtrace_print("Pass 3");
73 if (!(ctx->options & E2F_OPT_PREEN))
74 fix_problem(ctx, PR_3_PASS_HEADER, &pctx);
77 * Allocate some bitmaps to do loop detection.
79 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("inode done bitmap"),
83 fix_problem(ctx, PR_3_ALLOCATE_IBITMAP_ERROR, &pctx);
84 ctx->flags |= E2F_FLAG_ABORT;
88 if (ctx->options & E2F_OPT_TIME) {
89 e2fsck_clear_progbar(ctx);
90 print_resource_track(_("Peak memory"), &ctx->global_rtrack);
95 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
98 ext2fs_mark_inode_bitmap(inode_done_map, EXT2_ROOT_INO);
100 maxdirs = e2fsck_get_num_dirinfo(ctx);
104 if ((ctx->progress)(ctx, 3, 0, maxdirs))
107 for (i=0; (dir = e2fsck_dir_info_iter(ctx, &i)) != 0;) {
108 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
110 if (ctx->progress && (ctx->progress)(ctx, 3, count++, maxdirs))
112 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dir->ino))
113 if (check_directory(ctx, dir, &pctx))
118 * Force the creation of /lost+found if not present
120 if ((ctx->flags & E2F_OPT_READONLY) == 0)
121 e2fsck_get_lost_and_found(ctx, 1);
124 * If there are any directories that need to be indexed or
125 * optimized, do it here.
127 e2fsck_rehash_directories(ctx);
130 e2fsck_free_dir_info(ctx);
131 if (inode_loop_detect) {
132 ext2fs_free_inode_bitmap(inode_loop_detect);
133 inode_loop_detect = 0;
135 if (inode_done_map) {
136 ext2fs_free_inode_bitmap(inode_done_map);
140 #ifdef RESOURCE_TRACK
141 if (ctx->options & E2F_OPT_TIME2) {
142 e2fsck_clear_progbar(ctx);
143 print_resource_track(_("Pass 3"), &rtrack);
149 * This makes sure the root inode is present; if not, we ask if the
150 * user wants us to create it. Not creating it is a fatal error.
152 static void check_root(e2fsck_t ctx)
154 ext2_filsys fs = ctx->fs;
156 struct ext2_inode inode;
158 struct problem_context pctx;
160 clear_problem_context(&pctx);
162 if (ext2fs_test_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO)) {
164 * If the root inode is not a directory, die here. The
165 * user must have answered 'no' in pass1 when we
166 * offered to clear it.
168 if (!(ext2fs_test_inode_bitmap(ctx->inode_dir_map,
170 fix_problem(ctx, PR_3_ROOT_NOT_DIR_ABORT, &pctx);
171 ctx->flags |= E2F_FLAG_ABORT;
176 if (!fix_problem(ctx, PR_3_NO_ROOT_INODE, &pctx)) {
177 fix_problem(ctx, PR_3_NO_ROOT_INODE_ABORT, &pctx);
178 ctx->flags |= E2F_FLAG_ABORT;
182 e2fsck_read_bitmaps(ctx);
185 * First, find a free block
187 pctx.errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
189 pctx.str = "ext2fs_new_block";
190 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
191 ctx->flags |= E2F_FLAG_ABORT;
194 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
195 ext2fs_mark_block_bitmap(fs->block_map, blk);
196 ext2fs_mark_bb_dirty(fs);
199 * Now let's create the actual data block for the inode
201 pctx.errcode = ext2fs_new_dir_block(fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
204 pctx.str = "ext2fs_new_dir_block";
205 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
206 ctx->flags |= E2F_FLAG_ABORT;
210 pctx.errcode = ext2fs_write_dir_block(fs, blk, block);
212 pctx.str = "ext2fs_write_dir_block";
213 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
214 ctx->flags |= E2F_FLAG_ABORT;
217 ext2fs_free_mem(&block);
220 * Set up the inode structure
222 memset(&inode, 0, sizeof(inode));
223 inode.i_mode = 040755;
224 inode.i_size = fs->blocksize;
225 inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
226 inode.i_links_count = 2;
227 inode.i_blocks = fs->blocksize / 512;
228 inode.i_block[0] = blk;
231 * Write out the inode.
233 pctx.errcode = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
235 pctx.str = "ext2fs_write_inode";
236 fix_problem(ctx, PR_3_CREATE_ROOT_ERROR, &pctx);
237 ctx->flags |= E2F_FLAG_ABORT;
242 * Miscellaneous bookkeeping...
244 e2fsck_add_dir_info(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
245 ext2fs_icount_store(ctx->inode_count, EXT2_ROOT_INO, 2);
246 ext2fs_icount_store(ctx->inode_link_info, EXT2_ROOT_INO, 2);
248 ext2fs_mark_inode_bitmap(ctx->inode_used_map, EXT2_ROOT_INO);
249 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, EXT2_ROOT_INO);
250 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_ROOT_INO);
251 ext2fs_mark_ib_dirty(fs);
255 * This subroutine is responsible for making sure that a particular
256 * directory is connected to the root; if it isn't we trace it up as
257 * far as we can go, and then offer to connect the resulting parent to
258 * the lost+found. We have to do loop detection; if we ever discover
259 * a loop, we treat that as a disconnected directory and offer to
260 * reparent it to lost+found.
262 * However, loop detection is expensive, because for very large
263 * filesystems, the inode_loop_detect bitmap is huge, and clearing it
264 * is non-trivial. Loops in filesystems are also a rare error case,
265 * and we shouldn't optimize for error cases. So we try two passes of
266 * the algorithm. The first time, we ignore loop detection and merely
267 * increment a counter; if the counter exceeds some extreme threshold,
268 * then we try again with the loop detection bitmap enabled.
270 static int check_directory(e2fsck_t ctx, struct dir_info *dir,
271 struct problem_context *pctx)
273 ext2_filsys fs = ctx->fs;
274 struct dir_info *p = dir;
275 int loop_pass = 0, parent_count = 0;
282 * Mark this inode as being "done"; by the time we
283 * return from this function, the inode we either be
284 * verified as being connected to the directory tree,
285 * or we will have offered to reconnect this to
288 * If it was marked done already, then we've reached a
289 * parent we've already checked.
291 if (ext2fs_mark_inode_bitmap(inode_done_map, p->ino))
295 * If this directory doesn't have a parent, or we've
296 * seen the parent once already, then offer to
297 * reparent it to lost+found
301 (ext2fs_test_inode_bitmap(inode_loop_detect,
304 if (fix_problem(ctx, PR_3_UNCONNECTED_DIR, pctx)) {
305 if (e2fsck_reconnect_file(ctx, pctx->ino))
306 ext2fs_unmark_valid(fs);
308 p = e2fsck_get_dir_info(ctx, pctx->ino);
311 PR_3_NO_DIRINFO, pctx);
314 p->parent = ctx->lost_and_found;
315 fix_dotdot(ctx, p, ctx->lost_and_found);
320 p = e2fsck_get_dir_info(ctx, p->parent);
322 pctx->ino = p->parent;
323 fix_problem(ctx, PR_3_NO_DIRINFO, pctx);
327 ext2fs_mark_inode_bitmap(inode_loop_detect,
329 } else if (parent_count++ > 2048) {
331 * If we've run into a path depth that's
332 * greater than 2048, try again with the inode
333 * loop bitmap turned on and start from the
337 if (inode_loop_detect)
338 ext2fs_clear_inode_bitmap(inode_loop_detect);
340 pctx->errcode = ext2fs_allocate_inode_bitmap(fs, _("inode loop detection bitmap"), &inode_loop_detect);
344 PR_3_ALLOCATE_IBITMAP_ERROR, pctx);
345 ctx->flags |= E2F_FLAG_ABORT;
354 * Make sure that .. and the parent directory are the same;
355 * offer to fix it if not.
357 if (dir->parent != dir->dotdot) {
358 pctx->ino = dir->ino;
359 pctx->ino2 = dir->dotdot;
360 pctx->dir = dir->parent;
361 if (fix_problem(ctx, PR_3_BAD_DOT_DOT, pctx))
362 fix_dotdot(ctx, dir, dir->parent);
368 * This routine gets the lost_and_found inode, making it a directory
371 ext2_ino_t e2fsck_get_lost_and_found(e2fsck_t ctx, int fix)
373 ext2_filsys fs = ctx->fs;
377 struct ext2_inode inode;
379 static const char name[] = "lost+found";
380 struct problem_context pctx;
381 struct dir_info *dirinfo;
383 if (ctx->lost_and_found)
384 return ctx->lost_and_found;
386 clear_problem_context(&pctx);
388 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name,
389 sizeof(name)-1, 0, &ino);
393 if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, ino)) {
394 ctx->lost_and_found = ino;
398 /* Lost+found isn't a directory! */
402 if (!fix_problem(ctx, PR_3_LPF_NOTDIR, &pctx))
405 /* OK, unlink the old /lost+found file. */
406 pctx.errcode = ext2fs_unlink(fs, EXT2_ROOT_INO, name, ino, 0);
408 pctx.str = "ext2fs_unlink";
409 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
412 dirinfo = e2fsck_get_dir_info(ctx, ino);
415 e2fsck_adjust_inode_count(ctx, ino, -1);
416 } else if (retval != EXT2_ET_FILE_NOT_FOUND) {
417 pctx.errcode = retval;
418 fix_problem(ctx, PR_3_ERR_FIND_LPF, &pctx);
420 if (!fix_problem(ctx, PR_3_NO_LF_DIR, 0))
424 * Read the inode and block bitmaps in; we'll be messing with
427 e2fsck_read_bitmaps(ctx);
430 * First, find a free block
432 retval = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
434 pctx.errcode = retval;
435 fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
438 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
439 ext2fs_block_alloc_stats(fs, blk, +1);
442 * Next find a free inode.
444 retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040700,
445 ctx->inode_used_map, &ino);
447 pctx.errcode = retval;
448 fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
451 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
452 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
453 ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
456 * Now let's create the actual data block for the inode
458 retval = ext2fs_new_dir_block(fs, ino, EXT2_ROOT_INO, &block);
460 pctx.errcode = retval;
461 fix_problem(ctx, PR_3_ERR_LPF_NEW_DIR_BLOCK, &pctx);
465 retval = ext2fs_write_dir_block(fs, blk, block);
466 ext2fs_free_mem(&block);
468 pctx.errcode = retval;
469 fix_problem(ctx, PR_3_ERR_LPF_WRITE_BLOCK, &pctx);
474 * Set up the inode structure
476 memset(&inode, 0, sizeof(inode));
477 inode.i_mode = 040700;
478 inode.i_size = fs->blocksize;
479 inode.i_atime = inode.i_ctime = inode.i_mtime = ctx->now;
480 inode.i_links_count = 2;
481 inode.i_blocks = fs->blocksize / 512;
482 inode.i_block[0] = blk;
485 * Next, write out the inode.
487 pctx.errcode = ext2fs_write_new_inode(fs, ino, &inode);
489 pctx.str = "ext2fs_write_inode";
490 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
494 * Finally, create the directory link
496 pctx.errcode = ext2fs_link(fs, EXT2_ROOT_INO, name, ino, EXT2_FT_DIR);
498 pctx.str = "ext2fs_link";
499 fix_problem(ctx, PR_3_CREATE_LPF_ERROR, &pctx);
504 * Miscellaneous bookkeeping that needs to be kept straight.
506 e2fsck_add_dir_info(ctx, ino, EXT2_ROOT_INO);
507 e2fsck_adjust_inode_count(ctx, EXT2_ROOT_INO, 1);
508 ext2fs_icount_store(ctx->inode_count, ino, 2);
509 ext2fs_icount_store(ctx->inode_link_info, ino, 2);
510 ctx->lost_and_found = ino;
512 printf("/lost+found created; inode #%lu\n", ino);
518 * This routine will connect a file to lost+found
520 int e2fsck_reconnect_file(e2fsck_t ctx, ext2_ino_t ino)
522 ext2_filsys fs = ctx->fs;
525 struct problem_context pctx;
526 struct ext2_inode inode;
529 clear_problem_context(&pctx);
532 if (!ctx->bad_lost_and_found && !ctx->lost_and_found) {
533 if (e2fsck_get_lost_and_found(ctx, 1) == 0)
534 ctx->bad_lost_and_found++;
536 if (ctx->bad_lost_and_found) {
537 fix_problem(ctx, PR_3_NO_LPF, &pctx);
541 sprintf(name, "#%u", ino);
542 if (ext2fs_read_inode(fs, ino, &inode) == 0)
543 file_type = ext2_file_type(inode.i_mode);
544 retval = ext2fs_link(fs, ctx->lost_and_found, name, ino, file_type);
545 if (retval == EXT2_ET_DIR_NO_SPACE) {
546 if (!fix_problem(ctx, PR_3_EXPAND_LF_DIR, &pctx))
548 retval = e2fsck_expand_directory(ctx, ctx->lost_and_found,
551 pctx.errcode = retval;
552 fix_problem(ctx, PR_3_CANT_EXPAND_LPF, &pctx);
555 retval = ext2fs_link(fs, ctx->lost_and_found, name,
559 pctx.errcode = retval;
560 fix_problem(ctx, PR_3_CANT_RECONNECT, &pctx);
563 e2fsck_adjust_inode_count(ctx, ino, 1);
569 * Utility routine to adjust the inode counts on an inode.
571 errcode_t e2fsck_adjust_inode_count(e2fsck_t ctx, ext2_ino_t ino, int adj)
573 ext2_filsys fs = ctx->fs;
575 struct ext2_inode inode;
580 retval = ext2fs_read_inode(fs, ino, &inode);
585 printf("Adjusting link count for inode %lu by %d (from %d)\n", ino, adj,
586 inode.i_links_count);
590 ext2fs_icount_increment(ctx->inode_count, ino, 0);
591 if (inode.i_links_count == (__u16) ~0)
593 ext2fs_icount_increment(ctx->inode_link_info, ino, 0);
594 inode.i_links_count++;
595 } else if (adj == -1) {
596 ext2fs_icount_decrement(ctx->inode_count, ino, 0);
597 if (inode.i_links_count == 0)
599 ext2fs_icount_decrement(ctx->inode_link_info, ino, 0);
600 inode.i_links_count--;
603 retval = ext2fs_write_inode(fs, ino, &inode);
611 * Fix parent --- this routine fixes up the parent of a directory.
613 struct fix_dotdot_struct {
620 static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
621 int offset EXT2FS_ATTR((unused)),
622 int blocksize EXT2FS_ATTR((unused)),
623 char *buf EXT2FS_ATTR((unused)),
626 struct fix_dotdot_struct *fp = (struct fix_dotdot_struct *) priv_data;
628 struct problem_context pctx;
630 if ((dirent->name_len & 0xFF) != 2)
632 if (strncmp(dirent->name, "..", 2))
635 clear_problem_context(&pctx);
637 retval = e2fsck_adjust_inode_count(fp->ctx, dirent->inode, -1);
639 pctx.errcode = retval;
640 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
642 retval = e2fsck_adjust_inode_count(fp->ctx, fp->parent, 1);
644 pctx.errcode = retval;
645 fix_problem(fp->ctx, PR_3_ADJUST_INODE, &pctx);
647 dirent->inode = fp->parent;
650 return DIRENT_ABORT | DIRENT_CHANGED;
653 static void fix_dotdot(e2fsck_t ctx, struct dir_info *dir, ext2_ino_t parent)
655 ext2_filsys fs = ctx->fs;
657 struct fix_dotdot_struct fp;
658 struct problem_context pctx;
666 printf("Fixing '..' of inode %lu to be %lu...\n", dir->ino, parent);
669 retval = ext2fs_dir_iterate(fs, dir->ino, DIRENT_FLAG_INCLUDE_EMPTY,
670 0, fix_dotdot_proc, &fp);
671 if (retval || !fp.done) {
672 clear_problem_context(&pctx);
674 pctx.errcode = retval;
675 fix_problem(ctx, retval ? PR_3_FIX_PARENT_ERR :
676 PR_3_FIX_PARENT_NOFIND, &pctx);
677 ext2fs_unmark_valid(fs);
679 dir->dotdot = parent;
685 * These routines are responsible for expanding a /lost+found if it is
689 struct expand_dir_struct {
698 static int expand_dir_proc(ext2_filsys fs,
700 e2_blkcnt_t blockcnt,
701 blk_t ref_block EXT2FS_ATTR((unused)),
702 int ref_offset EXT2FS_ATTR((unused)),
705 struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
707 static blk_t last_blk = 0;
714 if (es->guaranteed_size && blockcnt >= es->guaranteed_size)
718 es->last_block = blockcnt;
723 retval = ext2fs_new_block(fs, last_blk, ctx->block_found_map,
730 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
736 retval = ext2fs_write_dir_block(fs, new_blk, block);
738 retval = ext2fs_get_mem(fs->blocksize, &block);
743 memset(block, 0, fs->blocksize);
744 retval = io_channel_write_blk(fs->io, new_blk, 1, block);
750 ext2fs_free_mem(&block);
752 ext2fs_mark_block_bitmap(ctx->block_found_map, new_blk);
753 ext2fs_block_alloc_stats(fs, new_blk, +1);
757 return (BLOCK_CHANGED | BLOCK_ABORT);
759 return BLOCK_CHANGED;
762 errcode_t e2fsck_expand_directory(e2fsck_t ctx, ext2_ino_t dir,
763 int num, int guaranteed_size)
765 ext2_filsys fs = ctx->fs;
767 struct expand_dir_struct es;
768 struct ext2_inode inode;
770 if (!(fs->flags & EXT2_FLAG_RW))
771 return EXT2_ET_RO_FILSYS;
774 * Read the inode and block bitmaps in; we'll be messing with
777 e2fsck_read_bitmaps(ctx);
779 retval = ext2fs_check_directory(fs, dir);
784 es.guaranteed_size = guaranteed_size;
790 retval = ext2fs_block_iterate2(fs, dir, BLOCK_FLAG_APPEND,
791 0, expand_dir_proc, &es);
797 * Update the size and block count fields in the inode.
799 retval = ext2fs_read_inode(fs, dir, &inode);
803 inode.i_size = (es.last_block + 1) * fs->blocksize;
804 inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
806 e2fsck_write_inode(ctx, dir, &inode, "expand_directory");